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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Nini1
Frequent Visitor

Taking max value per id with filters

Hello everyone,

 

I'm a new user of Power BI and need your help to solve the problem.

 

I have a data with the following fields: ID, ID2, date, date1, Amount. ID is unique for each customer, that may have different ID2. I was trying to find  sum of 'amount' for just only those cases and those ID2-s when date is less than  Min value that is created by me and is shown in script below and when date1 is more or equal to that min value. Also I want to sum just those values that have max ID2 per Id within this time restriction. (So trying to find max of ID2 for only those cases when date is less than min value and date1 is more or equal to min value)

 

Here is a sample data. (ID, ID2, Date, Date1, Amount) e.g. min date is 10/9/2021. I need to sum 'Amount' where 'Date'<mindate and 'Date1' >=mindate and for this time period we should sum 'amount' that has highest ID2.

e.g. for ID=3, we should take into account only 'amount' for ID2=13 and not 10 and 15 because 15 does not have 'Date' less than 'mindate' and 10 is not the highest ID2 for this customer with ID3.  Hope this will clarify the issue I have.

IDID2DateDate1Amountmin(date) - 10/9/2021result
129/29/202110/30/2021100 1001700
239/30/202110/30/2021500 0 
2410/5/202110/30/2021800 800 
31010/5/202111/15/2021200 0 
31310/8/202111/15/2021800 800 
31511/2/202111/15/20211000 0 
4231/31/20215/1/2021250 0 
4242/2/20215/1/2021300 0 


"A single value for column 'ID2' in table 'Tbl' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."
I wrote this script but it gives me this error  

 

 

Total Amount = 

VAR MIN_N_ = MIN('Tbl'[date])

VAR RESULT_N_ = CALCULATE(MAX('Tbl'[ID2],ALLEXCEPT('Tbl','Tbl'[ID])),FILTER(ALL('Tbl'[date]),'Tbl'[date]<MIN_N_),FILTER(ALL('Tbl'[date1]),'Tbl'[date1]>=MIN_N_))

VAR RESULT_Fin_ = CALCULATE(SUM('Tbl'[Amount]),FILTER(ALL('Tbl'[ID2]),'Tbl'[ID2]=RESULT_N_))

RETURN RESULT_Fin_

 

 

 

 

 

 Could you please tell me exactly how can I adjust the script or field so that it could perform without errors and give corresponding analytics?

 

Thank you in advance

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Nini1 ,

 

You said that the min date is created by yourself. If you want to make the min date dynamic, you can use the slicer.

 

Here's my solution.

1.Create a calendar table.

Calendar = CALENDAR(DATE(2021,1,1),DATE(2021,12,31))

There's no relationship.

vstephenmsft_0-1641892356781.png

 

2.Create the following measures.

Measure is to find the rows where the Date column is less than Min Date and the Date2 column is greater than Min Date.

Measure = IF(MAX('Table'[Date])<SELECTEDVALUE('Calendar'[Date])&&MAX('Table'[Date1])>SELECTEDVALUE('Calendar'[Date]),1)
Measure 2 = var _maxid=CALCULATE(MAX('Table'[ID2]),FILTER(ALL('Table'),[ID]=MAX('Table'[ID])&&[Measure]=1))
return CALCULATE(SUM('Table'[Amount]),FILTER('Table',[ID2]=_maxid))+0

Measure3 is to get the correct total value.

Measure 3 = SUMX('Table',[Measure 2])

vstephenmsft_1-1641892414940.png

 

 

 

Best Regards,

Stephen Tao

 

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

5 REPLIES 5
Anonymous
Not applicable

Hi @Nini1 ,

 

You said that the min date is created by yourself. If you want to make the min date dynamic, you can use the slicer.

 

Here's my solution.

1.Create a calendar table.

Calendar = CALENDAR(DATE(2021,1,1),DATE(2021,12,31))

There's no relationship.

vstephenmsft_0-1641892356781.png

 

2.Create the following measures.

Measure is to find the rows where the Date column is less than Min Date and the Date2 column is greater than Min Date.

Measure = IF(MAX('Table'[Date])<SELECTEDVALUE('Calendar'[Date])&&MAX('Table'[Date1])>SELECTEDVALUE('Calendar'[Date]),1)
Measure 2 = var _maxid=CALCULATE(MAX('Table'[ID2]),FILTER(ALL('Table'),[ID]=MAX('Table'[ID])&&[Measure]=1))
return CALCULATE(SUM('Table'[Amount]),FILTER('Table',[ID2]=_maxid))+0

Measure3 is to get the correct total value.

Measure 3 = SUMX('Table',[Measure 2])

vstephenmsft_1-1641892414940.png

 

 

 

Best Regards,

Stephen Tao

 

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

 

@Anonymous Thank you for you response. Yes I'm trying to create min myself just I'm creating it depending on 'Date' field. So I have slicer on 'Date' field I have in my table and was trying to change min (Date) when I change it on slicer. If I insert and substitute Calendar slicer you have with Date field slicer will these steps still work? or creating calendar table is the only way for dynamic min filter ?

Thank you very much

Anonymous
Not applicable

Hi @Nini1 ,

 

If the date field in the main table is used as a slicer filter.

You'll see that the visual is also filtered out, so that doesn't work.

vstephenmsft_0-1641967682691.png

 

We usually create a calendar table that has no relationship with the main table to get the dates you want to compare.

 

Best Regards,

Stephen Tao

 

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

 

smpa01
Super User
Super User

@Nini1  provide sample data, expected output.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Nini1
Frequent Visitor

@smpa01 

Here is a sample data. (ID, ID2, Date, Date1, Amount) e.g. min date is 10/9/2021. I need to sum 'Amount' where 'Date'<mindate and 'Date1' >=mindate and for this time period we should sum 'amount' that has highest ID2.

e.g. for ID=3, we should take into account only 'amount' for ID2=13 and not 10 and 15 because 15 does not have 'Date' less than 'mindate' and 10 is not the highest ID2 for this customer with ID3.  Hope this will clarify the issue I have.

 

IDID2DateDate1Amountmin(date) - 10/9/2021 result
129/29/202110/30/2021100 1001700
239/30/202110/30/2021500 0 
2410/5/202110/30/2021800 800 
31010/5/202111/15/2021200 0 
31310/8/202111/15/2021800 800 
31511/2/202111/15/20211000 0 
4231/31/20215/1/2021250 0 
4242/2/20215/1/2021300 0 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.