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
itsmeanuj
Helper IV
Helper IV

Max value based on dynamic date filter

I need to calculate something similar to the "partition by" function in SQL. 

 

My data contains multiple instances of IDs. I need to create a measure that would pick up the latest value of the "Call_flag" column (first calculate max "created_on" and then max "Stamp" column) for each unique ID & Product Combination. This means that the total count should show the count of unique ID&Product combinations. e.g. for Product A, the count of IDs would be 4 with the split of 3 "1s" (ID = 242977761642152961, 929999783480788916, 932585828936911719) and 1 "0"( ID = 932778067030379386".  

if the latest (first calculate max "created_on" and then max "Stamp" column) ID product combination has a value of 1 for "Call_flag" then, consider 1 and similarly 0 if the latest value is 0

 

PRODUCT_NAMEORGANIZATION_NAMEIDCHANGED_ONHAS_CALLSIS_TREATINGCALL_FLAGSTAMP
Product AA2429777616421529612020/04/200112020/04/21
Product AA2429777616421529612020/04/200102020/04/20
Product AA2429777616421529612020/03/200012020/04/22
Product AA2429777616421529612020/03/200002020/03/20
Product AB9299997834807889162021/05/031112021/05/04
Product AB9299997834807889162021/05/031112021/05/03
Product AB9299997834807889162021/04/030012021/04/04
Product AB9299997834807889162021/04/030012021/04/03
Product AD9325858289369117192023/09/020112023/09/03
Product AD9325858289369117192023/09/020112023/09/02
Product AD9325858289369117192023/02/021112023/02/03
Product AD9325858289369117192023/02/021112023/02/02
Product AC9327780670303793862022/07/160102022/07/17
Product AC9327780670303793862022/07/160102022/07/16
Product AC9327780670303793862022/04/161002022/04/17
Product AC9327780670303793862022/04/161002022/04/16
Product BA2429777616421529612020/04/210012020/04/22
Product BA2429777616421529612020/04/210012020/04/21
Product BA2429777616421529612020/03/020012020/03/03
Product BA2429777616421529612020/03/020012020/03/02
Product BB9299997834807889162021/05/311102021/06/01
Product BB9299997834807889162021/05/311102021/05/31
Product BB9299997834807889162021/04/081102021/04/09
Product BB9299997834807889162021/04/081102021/04/08
Product BD9325858289369117192023/09/100002023/09/11
Product BD9325858289369117192023/09/100002023/09/10
Product BD9325858289369117192023/02/221102023/02/23
Product BD9325858289369117192023/02/221102023/02/22
Product BC9327780670303793862022/07/100012022/07/11
Product BC9327780670303793862022/07/100012022/07/10
Product BC9327780670303793862022/04/140112022/04/15
Product BC9327780670303793862022/04/140112022/04/14
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @itsmeanuj ,

 

Here are the steps you can follow:

1. Create measure.

Measure =
var _maxdatecreateon=
MAXX(
    FILTER(ALLSELECTED('Table'),
    'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])&&'Table'[ID]=MAX('Table'[ID])),[CHANGED_ON])
var _maxstamp=
MAXX(
    FILTER(ALLSELECTED('Table'),     'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])&&'Table'[ID]=MAX('Table'[ID])&&'Table'[CHANGED_ON]=_maxdatecreateon),'Table'[STAMP])
return
MAXX(
    FILTER(ALL('Table'),    'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])&&'Table'[ID]=MAX('Table'[ID])&&'Table'[CHANGED_ON]=_maxdatecreateon&&'Table'[STAMP]=_maxstamp),'Table'[CALL_FLAG])
count of IDs =
CALCULATE(
    DISTINCTCOUNT(
    'Table'[ID]),FILTER(ALL('Table'),'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])))

2. Result:

vyangliumsft_0-1692328521213.png

 

Best Regards,

Liu Yang

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

3 REPLIES 3
Anonymous
Not applicable

Hi  @itsmeanuj ,

 

Here are the steps you can follow:

1. Create measure.

Measure =
var _maxdatecreateon=
MAXX(
    FILTER(ALLSELECTED('Table'),
    'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])&&'Table'[ID]=MAX('Table'[ID])),[CHANGED_ON])
var _maxstamp=
MAXX(
    FILTER(ALLSELECTED('Table'),     'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])&&'Table'[ID]=MAX('Table'[ID])&&'Table'[CHANGED_ON]=_maxdatecreateon),'Table'[STAMP])
return
MAXX(
    FILTER(ALL('Table'),    'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])&&'Table'[ID]=MAX('Table'[ID])&&'Table'[CHANGED_ON]=_maxdatecreateon&&'Table'[STAMP]=_maxstamp),'Table'[CALL_FLAG])
count of IDs =
CALCULATE(
    DISTINCTCOUNT(
    'Table'[ID]),FILTER(ALL('Table'),'Table'[PRODUCT_NAME]=MAX('Table'[PRODUCT_NAME])))

2. Result:

vyangliumsft_0-1692328521213.png

 

Best Regards,

Liu Yang

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

amitchandak
Super User
Super User

@itsmeanuj , if you need a column you can use earlier

 

maxx(filter(Table, Table[ID] = earlier(Table[ID])) , Table[Date])

 

refer

Earlier, I should have known Earlier: https://youtu.be/CVW6YwvHHi8

 

 

Or you can consider the following for measures

 

Latest
https://amitchandak.medium.com/power-bi-get-the-last-latest-value-of-a-category-d0cf2fcf92d0

https://amitchandak.medium.com/power-bi-get-the-sum-of-the-last-latest-value-of-a-category-f1c839ee8...

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak - Thanks for your response. But in my scenario, we first need to take latest "changed_on" date and then latest "Stamp" date in that "Changed_on" date. Also this needs to be dynamic as there is a "changed_on" date slicer at the top of the page.

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.