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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
lucasattom
New Member

Return the amount for max date for each item and sum for group

Hello,

 

I'm having some trouble getting the following table to work.

 

I have the following database (it's simplified, I actually have information in different connected tables 😞

 

lucasattom_2-1683904617387.png

 

I want a matrix table summarized by the amount of each product for the max date (and within that date, I want the row with the max "Id"). The highlighted rows are the desired ones for the output.

 

The resulting matrix should look like this:

 

lucasattom_1-1683904385018.png

 

So I'd have a measure that returns the most recent amount (highest "date", then highest "Id") for each product, and summarize the group with the sum for each item. I tried using SUMMARIZE with no success.

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @lucasattom ,

 

I think you can try this code to create a measure.

Output =
VAR _AddColumn =
    ADDCOLUMNS (
        FILTER ( 'Table', 'Table'[Date] = MAX ( 'Table'[Date] ) ),
        "MAXID",
            CALCULATE (
                MAX ( 'Table'[Id] ),
                FILTER (
                    ALLEXCEPT ( 'Table', 'Table'[Category], 'Table'[Product] ),
                    'Table'[Date] = MAX ( 'Table'[Date] )
                )
            )
    )
VAR _Filter =
    FILTER ( _AddColumn, [Id] = [MAXID] )
RETURN
    SUMX ( _Filter, [Amount] )

Result is as below.

vrzhoumsft_0-1684135618424.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-rzhou-msft
Community Support
Community Support

Hi @lucasattom ,

 

I think you can try this code to create a measure.

Output =
VAR _AddColumn =
    ADDCOLUMNS (
        FILTER ( 'Table', 'Table'[Date] = MAX ( 'Table'[Date] ) ),
        "MAXID",
            CALCULATE (
                MAX ( 'Table'[Id] ),
                FILTER (
                    ALLEXCEPT ( 'Table', 'Table'[Category], 'Table'[Product] ),
                    'Table'[Date] = MAX ( 'Table'[Date] )
                )
            )
    )
VAR _Filter =
    FILTER ( _AddColumn, [Id] = [MAXID] )
RETURN
    SUMX ( _Filter, [Amount] )

Result is as below.

vrzhoumsft_0-1684135618424.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors