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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Pawel_C
Frequent Visitor

Select two latest values per attribute

Hi


I'm really rookie in terms of using DAX and PowerQuery, but need to solve a problem. 
Having a table with data for each attribute in a daily manner:
(note! not all attributes are reported every day)

DATECategoryValue
8/4/2023Beef123
8/3/2023Sheep456
8/3/2023Beef789
8/2/2023Sheep1234
8/1/2023Beef2345
8/1/2023Sheep4567

 

For each category I need to get to information on values from two latest available dates. (Format doesn't matter for now, I can pivot/unpivot the data to my needs).

DateAttributeValue
latest available dateBeef123
2nd latest available dateBeef789
latest available dateSheep456
2nd latest available dateSheep1234

 

Specific dates are "nice to have" but not necesary for this excersice. 
I have tried to sort the data by date first and find dax formula to add the column with the number of occurence of each category, but ChatGPT wasn't very helpful with that and threw at me solutions which didn't work.

Do any of you have an idea to solve it?

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Insert this step where #"Changed Type" should be replaced with your previous step

 

= Table.Combine(Table.Group(#"Changed Type", {"Category"}, {{"All", each Table.FromColumns(Table.ToColumns(Table.MaxN(_, "DATE", 2)) & {{"Latest available date", "2nd latest available date"}}, Table.ColumnNames(_)&{"Date Text"})}})[All])

 

Complete code in action

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WstA30TcyMDJW0lFySk1NA1KGQE6sDkjGGCYTnJGaWgCkTUzN0KWgmswtLKEyRuiagOaZQOUM0XQBZUzRpZDsMleKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t, Category = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"Category", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Combine(Table.Group(#"Changed Type", {"Category"}, {{"All", each Table.FromColumns(Table.ToColumns(Table.MaxN(_, "DATE", 2)) & {{"Latest available date", "2nd latest available date"}}, Table.ColumnNames(_)&{"Date Text"})}})[All])
in
    #"Grouped Rows"

 

View solution in original post

8 REPLIES 8
Vijay_A_Verma
Super User
Super User

Insert this step where #"Changed Type" should be replaced with your previous step

 

= Table.Combine(Table.Group(#"Changed Type", {"Category"}, {{"All", each Table.FromColumns(Table.ToColumns(Table.MaxN(_, "DATE", 2)) & {{"Latest available date", "2nd latest available date"}}, Table.ColumnNames(_)&{"Date Text"})}})[All])

 

Complete code in action

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WstA30TcyMDJW0lFySk1NA1KGQE6sDkjGGCYTnJGaWgCkTUzN0KWgmswtLKEyRuiagOaZQOUM0XQBZUzRpZDsMleKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t, Category = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"Category", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Combine(Table.Group(#"Changed Type", {"Category"}, {{"All", each Table.FromColumns(Table.ToColumns(Table.MaxN(_, "DATE", 2)) & {{"Latest available date", "2nd latest available date"}}, Table.ColumnNames(_)&{"Date Text"})}})[All])
in
    #"Grouped Rows"

 

Hi Vijay,

Things got much more complicated for my selection.
On top of the 2 latest, I need to select "week ago" from the lastest found.
It's a problem I could solve IF this "week ago" day was always available. Sometimes it's not and I need to go 8 or 9 days back. 
Do you think you can help me with that?

I will need the sample data and output the way you gave in problem.

I need a selection of rows like in the example below (no need to pivot, I can do that myself).

desired outputdesired output

Here's my sample data

sample inputsample input

Thank you @Vijay_A_Verma!
Wonderful solution!

Pawel_C
Frequent Visitor

@Mahesh0016 Thank you for reaching out.
Using my example above, I have 2 categories: "Beef" and "Sheep". Both reported irregulary in the format as in my input table.
I want to find for each of them, the latest value, and the one before it.
So e.g. "Sheep" wasn't reported on 8/4/2023, so it's latest report would be from 8/3/2023 with value 789. Sheep was also reported o on 8/2/2023 with value 1234, so I would like to get that as well.
Repeat the process for each category.
For clarity, let's assume that this is my final output table:

Categorylatest datelatest value2nd latest date2nd latest value
Beef8/4/20231238/3/2023789
Sheep8/3/20237898/2/20231234

 

Hope that helps!

Mahesh0016
Super User
Super User

@Pawel_C  you want to below SS Result or...?

Mahesh0016_0-1691408250326.png

@Pawel_C  Thank You!!

Mahesh0016
Super User
Super User

@Pawel_C Please Can you Elaborate your EndOutPut? ThankYou!!

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors