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