Search code examples
powerbidax

How to add expression value into datatable column in power bi dax


I am trying to create a table from dax function Datatable.

I know I can create static table from datatable, but I want to add sum of sales into column of datatable function.

I'm trying to do like this:

data table = 
Var total = sum('Orders'[Sales])
Return
DATATABLE(
    "name",STRING,
    "amount",INTEGER,
    "city",STRING,
    {
        {"ram", total, "mohali"},
        {"karan", 25, "mohali"},
        {"atul", 25, "mohali"},
        {"vaibhav", 25, "mohali"}
    }
)

Here total is some of sale which i want to add in column...but I get this error:

The tuple at index '1' from the table definition of the DATATABLE function does not have a constant expression in the column at index '2'.

Can I add the total in datatable column? Thanks in advance


Solution

  • Try to put your datatable in a variable and put 0 as value for the record where you have the name = ram :

    data table = 
    VAR T1 =
    DATATABLE(
        "name", STRING,
        "amount", INTEGER,
        "city", STRING,
        {
            {"ram", 0, "mohali"},  
            {"karan", 25, "mohali"},
            {"atul", 25, "mohali"},
            {"vaibhav", 25, "mohali"}
        }
    )
    
    RETURN  
    SELECTCOLUMNS(
        T1,
        "name", [name],
        "amount", IF([name] = "ram", SUM('Orders'[Sales]), [amount]),
        "city", [city]
    )
    

    I tested the table based on the Contoso DWH and it is working :

    enter image description here