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
ToNo87
Regular Visitor

How to find a total count of multiple row where one value is distinct filtered on latest date

I have a table like this

 

HashId (text) | EventTime (date/time)| Amount

dsfa3r43         12/12/2019 12:04:16     5

dsfa3r43         12/12/2019 12:04:16     5

as23radf         12/11/2019 12:04:16     3

dsfa3r43         12/10/2019 12:04:16     2

 

I want to do a total count of the amount, but only where there is a distinct HashId and the latest EventTime.

 

So the result should be: 8 based on the fact that I have two unique HashIds, and the latest dates gives me a total of 8.

 

1 ACCEPTED SOLUTION
Vvelarde
Community Champion
Community Champion

@ToNo87

 

hi. a solution would be this:

LastSums =
SUMX (
    SUMMARIZE (
        Table1,
        Table1[HashId (text) ],
        "MAX", MAX ( Table1[ EventTime (date/time)] ),
        "AMOUNT", MAX ( Table1[ Amount] )
    ),
    [AMOUNT]
)



Lima - Peru

View solution in original post

5 REPLIES 5
Vvelarde
Community Champion
Community Champion

@ToNo87

 

hi. a solution would be this:

LastSums =
SUMX (
    SUMMARIZE (
        Table1,
        Table1[HashId (text) ],
        "MAX", MAX ( Table1[ EventTime (date/time)] ),
        "AMOUNT", MAX ( Table1[ Amount] )
    ),
    [AMOUNT]
)



Lima - Peru

Thanks @Vvelarde, but wont the last MAX just give me the highest and not the total sum of all the values in the Amount column?

Vvelarde
Community Champion
Community Champion

@ToNo87

 

hi, the SUMX aggregate all the max value from each Hash id.

 

HashId (text) | EventTime (date/time)| Amount

dsfa3r43         12/12/2019 12:04:16     5

dsfa3r43         12/12/2019 12:04:16     5

as23radf         12/11/2019 12:04:16     3

dsfa3r43         12/10/2019 12:04:16     2

 

Max take the Max EventTime of dsfa3r43, that is 12/12/2019 12:04:16 and the Max Amount (5), if you want both (5+5) Just change Max to Sum. The same repeat to as23radf . (EventTime: 12/11/2019 12:04:16   and MaxValue  is 3)

 

Let me know if works.

 

 

 




Lima - Peru

I believe the following measure will work. You should test it though. 

 

 

=sumx(values(tablename[hashid]),calculate(calculate(sum(tablename[amount]),lastdate(tablename[eventtime]))))

 

note this data will not compress well. If the table is small (say less than 500k rows) it won't be an issue. If it is big table, the file size may be very large. 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

Thanks, I got the following error though:

 


MdxScript(Model) (1, 140) Calculation error in measure 'Table'[Value]: A date column containing duplicate dates was specified in the call to function 'LASTDATE'. This is not supported.

 

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.