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

Group by Id, category and show last value in the month Power Query

Hi 

 

Here is my problem. I have a data set that has ID , Category , Date Submitted and the score.

Is there a way to group those scores by ID, Category, Month and show last value(the last submitted by date) in the month for each of the months ? 

 

I have tried to follow this turorial 

https://radacad.com/grouping-in-power-query-getting-the-last-item-in-each-group.

 

But the result I get is Max value not last value.

Here is an example :

This score is the Max score of this category not the last.

Photo 1.jpg

 

The last score should be zero as seen here

Photo 2.jpg

Here is the the query I have used

QueryQuery

Same happens when if I only leave the month column.

Photo 4.jpg

I would appreciate any help, and thank you in advance.

 

1 ACCEPTED SOLUTION

the code worked fine the only problem was that the sort was Descending with List.Last

I changed it to Ascending, and it works fine

let
    Source = Excel.Workbook(File.Contents("Sample.xlsx"), null, true),
    Sample_Sheet = Source{[Item="Sample",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sample_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"CustomerId", Int64.Type}, {"ResponseFamily", type text}, {"Scoring", Int64.Type}, {"ResponseDate", type date}}),
    #"Inserted Month" = Table.AddColumn(#"Changed Type", "Month", each Date.Month([ResponseDate]), Int64.Type),
    #"Inserted Year" = Table.AddColumn(#"Inserted Month", "Year", each Date.Year([ResponseDate]), Int64.Type),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Inserted Year", {{"Month", type text}, {"Year", type text}}, "en-US"),{"Month", "Year"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Month.1"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Merged Columns",{{"Month.1", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"ResponseDate", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"CustomerId", "ResponseFamily", "Month.1"}, {{"Date", each List.Max([ResponseDate]), type date}, {"Table", each _, type table [CustomerId=number, ResponseFamily=text, Scoring=number, ResponseDate=date, Month.1=date]},{"Last Score", each List.Last([Scoring]), type number}}),
    #"Sorted Rows1" = Table.Sort(#"Grouped Rows",{{"CustomerId", Order.Descending}})
in
    #"Sorted Rows1"

and it seems to work fine, but you can keep the Descending sort and use List.First instead

Capture.PNG



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

4 REPLIES 4
Stachu
Community Champion
Community Champion

Can you add sample tables (in format that can be copied to PowerBI) from your model with anonymised data? Like this (just copy and paste into the post window).

Column1Column2
A1
B2.5

please also include the M code that doesn't work for you



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Hi Stachu 

 

Thank you so much for your replay.

Here is the M code I have used 

let
    Source = Excel.Workbook(File.Contents("Sample.xlsx"), null, true),
    Sample_Sheet = Source{[Item="Sample",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sample_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"CustomerId", Int64.Type}, {"ResponseFamily", type text}, {"Scoring", Int64.Type}, {"ResponseDate", type date}}),
    #"Inserted Month" = Table.AddColumn(#"Changed Type", "Month", each Date.Month([ResponseDate]), Int64.Type),
    #"Inserted Year" = Table.AddColumn(#"Inserted Month", "Year", each Date.Year([ResponseDate]), Int64.Type),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Inserted Year", {{"Month", type text}, {"Year", type text}}, "en-US"),{"Month", "Year"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Month.1"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Merged Columns",{{"Month.1", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"ResponseDate", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"CustomerId", "ResponseFamily", "Month.1"}, {{"Date", each List.Max([ResponseDate]), type date}, {"Table", each _, type table [CustomerId=number, ResponseFamily=text, Scoring=number, ResponseDate=date, Month.1=date]},{"Last Score", each List.Last([Scoring]), type number}}),
    #"Sorted Rows1" = Table.Sort(#"Grouped Rows",{{"CustomerId", Order.Descending}})
in
    #"Sorted Rows1"

And here is the link for the sample file 

https://www.dropbox.com/s/s7s5eupal5ecpjz/Sample.xlsx?dl=0

 

Thank you for your time. 

 

the code worked fine the only problem was that the sort was Descending with List.Last

I changed it to Ascending, and it works fine

let
    Source = Excel.Workbook(File.Contents("Sample.xlsx"), null, true),
    Sample_Sheet = Source{[Item="Sample",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sample_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"CustomerId", Int64.Type}, {"ResponseFamily", type text}, {"Scoring", Int64.Type}, {"ResponseDate", type date}}),
    #"Inserted Month" = Table.AddColumn(#"Changed Type", "Month", each Date.Month([ResponseDate]), Int64.Type),
    #"Inserted Year" = Table.AddColumn(#"Inserted Month", "Year", each Date.Year([ResponseDate]), Int64.Type),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Inserted Year", {{"Month", type text}, {"Year", type text}}, "en-US"),{"Month", "Year"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Month.1"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Merged Columns",{{"Month.1", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"ResponseDate", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"CustomerId", "ResponseFamily", "Month.1"}, {{"Date", each List.Max([ResponseDate]), type date}, {"Table", each _, type table [CustomerId=number, ResponseFamily=text, Scoring=number, ResponseDate=date, Month.1=date]},{"Last Score", each List.Last([Scoring]), type number}}),
    #"Sorted Rows1" = Table.Sort(#"Grouped Rows",{{"CustomerId", Order.Descending}})
in
    #"Sorted Rows1"

and it seems to work fine, but you can keep the Descending sort and use List.First instead

Capture.PNG



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Hi 

Thank you so much, it worked like a charm. 

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
Top Kudoed Authors