cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ziqh Frequent Visitor
Frequent Visitor

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 7
rajendran Super Contributor
Super Contributor

Re: Summarising rows in calculated table

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

rajendran Super Contributor
Super Contributor

Re: Summarising rows in calculated table

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 Frequent Visitor
Frequent Visitor

Re: Summarising rows in calculated table

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.

rajendran Super Contributor
Super Contributor

Re: Summarising rows in calculated table

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

ziqh Frequent Visitor
Frequent Visitor

Re: Summarising rows in calculated table

Hi Raj,

 

Source for the two tables is a simple ODATA query.

Data itself is a bit more complex, but boils down to "ID", "Qty A" and "Qty B":

Untitled.png

 

The summarise function does not seem to accept any fields from the temporary table.

Also tried simply wrapping then UNION() in a SUMMARIZE() block, but the same occurred.

 

Glad for any advice!

v-jiascu-msft Super Contributor
Super Contributor

Re: Summarising rows in calculated table

Hi @ziqh,

 

There could be two methods without SUMMARIZE. Please check out the demo in the attachment.

Method 1 =
ADDCOLUMNS (
    DISTINCT ( UNION ( VALUES ( Table1[ID] ), VALUES ( Table2[ID] ) ) ),
    "Qty A", SUMX ( FILTER ( Table1, Table1[ID] = EARLIER ( Table1[ID] ) ), [Qty A] ),
    "Qty B", SUMX ( FILTER ( 'Table2', 'Table2'[ID] = EARLIER ( Table1[ID] ) ), [Qty B] )
)

Method 2: create a new table full of all ids and then add two columns.

Method 2 =
DISTINCT ( UNION ( VALUES ( Table1[ID] ), VALUES ( Table2[ID] ) ) )

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
v-jiascu-msft Super Contributor
Super Contributor

Re: Summarising rows in calculated table

Hi @ziqh,

 

Could you please mark the proper answers as solutions?

 

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Users Online
Currently online: 297 members 3,582 guests
Please welcome our newest community members: