Forum Discussion

ziqh's avatar
ziqh
Frequent Visitor
8 years ago

Summarising rows in calculated table

Hi All,

  Stuck on an issue that I'm almost certain is a simple one.

 

I have two tables of data, and I've taken them to a single calculated table via a UNION, like so:

CalcTable = UNION(

            SELECTCOLUMNS(Table1, "ID", [ID], "Qty A", [Quantity], "Qty B", 0),

            SELECTCOLUMNS(Table1, "ID", [ID], "Qty A", 0, "Qty B", [Quantity])

}

 

This has given me something like the following:

IDQty AQty B
1120
1010
2020
260
390

 

What I'm looking to end up with is:

IDQty AQty B
11210
2620
390

 

I thought the function for this was SUMMARIZE, but this appears to have no effect on the data.

 

Thanks for any ideas!

7 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Something like this should work for you.

    Calc_table= VAR  inTempTable= UNION(
                SELECTCOLUMNS(Table1, "ID", [ID], "Qty A", [Quantity], "Qty B", 0),
                SELECTCOLUMNS(Table1, "ID", [ID], "Qty A", 0, "Qty B", [Quantity])
                    )
              RETURN
              SUMMARIZE ( inTempTable, inTempTable[ID],"Total Qty A",SUM(inTempTable[QtyA]),"Total QtyB",SUM(inTempTable[QtyB]))

     

     

     

    Thanks

    Raj

     

    • ziqh's avatar
      ziqh
      Frequent Visitor

      Hi Raj, thanks for the help.

       

      I've tried the suggested, but when attempting to SUM(inTempTable[QtyA]), PowerBI refuses.

      Error:

      Table variable "inTempTable" cannot be used in this context because a base table is expected.

       

      The only fields it gives me access to in the SUM() are from other tables.

       

      Thanks.

      • Anonymous's avatar
        Anonymous
        Not applicable

        I think am confused. I see your expected output in your post, can you please post the source data of the two tables?

         

        Thanks

        Raj

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI ziqh

     

    The UNIOIN function just combines teh data and it wont aggregate, so the output of UNION is expected one.

     

    For your desired result, you need to Wrap your UNIONed query with a SUMMARIZE function.

     

    Thanks

    Raj

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft Employee

    Hi ziqh,

     

    Could you please mark the proper answers as solutions?

     

     

    Best Regards,

    Dale