Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Combine the same date and sum up values

Hi Everyone,

 

I have a table with grouped Asset ID, and corresponding Date, reading. I want to combine the same date, but sum up the reading. How can I do it in Power query or by DAX?

 

Thank you for helping!

 

 

babyjb123_1-1652813452446.png

 

 

2 ACCEPTED SOLUTIONS
rohit_singh
Solution Sage
Solution Sage

Hello @Anonymous ,

This can be done using either Power query or DAX. I have shown both methods below:

1) Power Query

This is the sample input :

rohit_singh_5-1652820113459.png


Perform the group by operation on columns ID and date, and summarize the reading column.

rohit_singh_0-1652819574935.png

This is the output : 

rohit_singh_2-1652819610171.png

Here is a sample code snippet:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsrQwMTdS0lEyMtc3MNQ3MjACc4xNzJRidXDIGqJIGRrrGxjBNZqZokgaG+obGMMkTcwtlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Reading = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date", type date}, {"Reading", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "Date"}, {{"Reading", each List.Sum([Reading]), type nullable number}})
in
#"Grouped Rows"

 

2) DAX

This is the sample input table :

rohit_singh_3-1652819668008.png

Use the "summarize" function in DAX to create a new table and group columns.

rohit_singh_4-1652819751485.png

Summarized readings =

SUMMARIZE('GroupByExample-DAX',
'GroupByExample-DAX'[ID],
'GroupByExample-DAX'[Date],
"Reading", SUM('GroupByExample-DAX'[Reading])
)
 

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

View solution in original post

Hi @Anonymous ,

I cannot understand why you would want to sort the order in the data view, unless it is a specific requirement of course!
Usually, you would add your fields to a visual on the report view, and then apply the desired sort 

rohit_singh_1-1652892617426.png

You also have the option to export this data.

If this does not work for you, then you can try adding an index column to your data once you have completed the sorting. Ordering the index column will ensure that your data remains in the same state as it was when you added the index.

rohit_singh_2-1652892851961.png

 

rohit_singh_3-1652892891219.png

 

rohit_singh_4-1652892964410.png

 


Kind regards,

Rohit

 

 

View solution in original post

8 REPLIES 8
rohit_singh
Solution Sage
Solution Sage

Hi @Anonymous ,

No worries. As an example, I have added another asset id to the sample data as shown below. You will notice that the dates are not sorted correctly.

rohit_singh_0-1652873846223.png


You simply need to add another step in power query that sorts your table first by assetid, and then by dates in ascending order. This will ensure that all assetid's are grouped together, and their dates are in order.

rohit_singh_1-1652873925017.png

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dcu7DcAgDADRXVwj+QvGs6Dsv0YCCiguUp6ebgyIbi5QQByJUUhWqDW4yo9yIlYkOWOrCZWRdKN539h8nh0pJvI8JTLK52SxjI5M5yTPGO+5sD543Q==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Reading = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date", type date}, {"Reading", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "Date"}, {{"Reading", each List.Sum([Reading]), type nullable number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"ID", Order.Ascending}, {"Date", Order.Ascending}})
in
#"Sorted Rows"


Kind regards,

Rohit 

Appreciate your kudos! 😊

Anonymous
Not applicable

Hi Rohit,

 

Thank you for your help! I followed your instruction, it works very well. But I am facing another problem. Once I save and closed the PQ, the order is messed up again! 😞
I tried using Table.Buffer=(#"Sorted Rows") to lock the order, but failed. I am stuck here , I really need help!

 

babyjb123_0-1652890768228.pngbabyjb123_1-1652890893634.png

 

Hi @Anonymous ,

I cannot understand why you would want to sort the order in the data view, unless it is a specific requirement of course!
Usually, you would add your fields to a visual on the report view, and then apply the desired sort 

rohit_singh_1-1652892617426.png

You also have the option to export this data.

If this does not work for you, then you can try adding an index column to your data once you have completed the sorting. Ordering the index column will ensure that your data remains in the same state as it was when you added the index.

rohit_singh_2-1652892851961.png

 

rohit_singh_3-1652892891219.png

 

rohit_singh_4-1652892964410.png

 


Kind regards,

Rohit

 

 

Anonymous
Not applicable

Hi Rohit,

 

Adding index works the best!  I really appreciate your help!!!

Anonymous
Not applicable

I need to do more calculation with DAX in the data view(not good at M in PQ :)). I need to calculate the date different of each record and find out the average.

Hi @Anonymous ,

No worries. Maybe this helps : 

rohit_singh_1-1652896602757.png

 

I have added the following columns assuming the calculation is for each id

Prev Date =

CALCULATE(
MAX(GroupByExample[Date]),
FILTER(GroupByExample, GroupByExample[ID] = EARLIER(GroupByExample[ID]) &&
GroupByExample[Date] < EARLIER(GroupByExample[Date])
))

 

DateDiff = DATEDIFF(GroupByExample[Prev Date], GroupByExample[Date], day)
 
Avg Date Diff =
CALCULATE(ROUND(AVERAGE(GroupByExample[DateDiff]),0),
FILTER(GroupByExample, GroupByExample[ID] = EARLIER(GroupByExample[ID])
)
)
 
Not sure if this is what you were looking to do but I hope it helps.

Kind regards,

Rohit
rohit_singh
Solution Sage
Solution Sage

Hello @Anonymous ,

This can be done using either Power query or DAX. I have shown both methods below:

1) Power Query

This is the sample input :

rohit_singh_5-1652820113459.png


Perform the group by operation on columns ID and date, and summarize the reading column.

rohit_singh_0-1652819574935.png

This is the output : 

rohit_singh_2-1652819610171.png

Here is a sample code snippet:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsrQwMTdS0lEyMtc3MNQ3MjACc4xNzJRidXDIGqJIGRrrGxjBNZqZokgaG+obGMMkTcwtlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Reading = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date", type date}, {"Reading", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "Date"}, {{"Reading", each List.Sum([Reading]), type nullable number}})
in
#"Grouped Rows"

 

2) DAX

This is the sample input table :

rohit_singh_3-1652819668008.png

Use the "summarize" function in DAX to create a new table and group columns.

rohit_singh_4-1652819751485.png

Summarized readings =

SUMMARIZE('GroupByExample-DAX',
'GroupByExample-DAX'[ID],
'GroupByExample-DAX'[Date],
"Reading", SUM('GroupByExample-DAX'[Reading])
)
 

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

Anonymous
Not applicable

Hi Rohit,

 

Thank you so much for your reply!!! The problem seems addressed, but there is is still a issure: the order of the date is messed up, how can i sort it keeping the same Asset ID? I am using the first method which group in Power query.

 

babyjb123_1-1652824044131.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors