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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ceov95
Regular Visitor

Retrieve a specific value with conditions

So I have this kind of data:

 

Item CodeQty StockDB Server DateMonth
Item 11020/3/2020March
Item 22015/3/2020March
Item 33018/3/2020March
Item 1823/3/2020March
Item 32428/3/2020March
Item 21517/3/2020March
Item 3203/4/2020April
Item 1510/4/2020April
Item 21216/4/2020April
Item 21025/4/2020April
Item 1426/4/2020April
Item 31829/4/2020April
Item 1215/5/2020May
Item 2719/5/2020May
Item 31219/5/2020May
Item 2426/5/2020May
Item 1126/5/2020May
Item 3927/5/2020May

 

And I need to find for each item, and for each month, the max date so I can retrieve the stock quantity, like this:

 

Item CodeQty StockDB Server DateMonth
Item 1823/3/2020March
Item 32428/3/2020March
Item 21517/3/2020March
Item 21025/4/2020April
Item 1426/4/2020April
Item 31829/4/2020April
Item 2426/5/2020May
Item 1126/5/2020May
Item 3927/5/2020May

 

I've tried doing in a way but was successful just for one Item but when I removed the filter for all the items it only brings me the max date without considering the Item Code. 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

Try this code. It returns this table. I just happened to write about this in a blog post this week.

edhans_0-1595006177777.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZLLCsIwEEV/RbIuJJ1pTLt06cIvKF2ICAoKUtz49/ZeH6XizGYIyeE+hvR92N6P11UdqlCnaUiKGiUJzrv9eDiFoXozwmeA2WR0ulIyrcnAq4WWujLSYNgywiQYxdfBvcbmg2xu4/myTEOVZCJ04lj7DBeYXSuWsmWUqwPTuTKv8jHPvR/LLAVAZwD6LWQRMkf9D/DDeAA8OgDlBxie", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Code" = _t, #"Qty Stock" = _t, #"DB Server Date" = _t, Month = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Qty Stock", Int64.Type}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"DB Server Date", type date}}, "en-BS"),
    #"Grouped Rows" = 
        Table.Group(
            #"Changed Type with Locale", 
            {"Item Code", "Month"}, 
            {
                {"Latest Date", each List.Max([DB Server Date]), type nullable date}, 
                {"Stock Amount", each Table.Max(_, "DB Server Date")[Qty Stock], Int64.Type}
            }
        )


in
    #"Grouped Rows"

 

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

Try this code. It returns this table. I just happened to write about this in a blog post this week.

edhans_0-1595006177777.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZLLCsIwEEV/RbIuJJ1pTLt06cIvKF2ICAoKUtz49/ZeH6XizGYIyeE+hvR92N6P11UdqlCnaUiKGiUJzrv9eDiFoXozwmeA2WR0ulIyrcnAq4WWujLSYNgywiQYxdfBvcbmg2xu4/myTEOVZCJ04lj7DBeYXSuWsmWUqwPTuTKv8jHPvR/LLAVAZwD6LWQRMkf9D/DDeAA8OgDlBxie", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Code" = _t, #"Qty Stock" = _t, #"DB Server Date" = _t, Month = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Qty Stock", Int64.Type}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"DB Server Date", type date}}, "en-BS"),
    #"Grouped Rows" = 
        Table.Group(
            #"Changed Type with Locale", 
            {"Item Code", "Month"}, 
            {
                {"Latest Date", each List.Max([DB Server Date]), type nullable date}, 
                {"Stock Amount", each Table.Max(_, "DB Server Date")[Qty Stock], Int64.Type}
            }
        )


in
    #"Grouped Rows"

 

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thanks man it helped me a lot!!!!

Great @ceov95. Glad my solution helped you out.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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 Solution Authors