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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Smitu30
New Member

How to dynamically number UserID and PurchaseDate

Hello.

 

Please see the below sample table.

UserIDPurchaseDate
0000012020-07-01
0000012020-07-12
0000012020-08-05
0000012020-08-21
0000022020-07-09
0000022020-08-06
0000022020-08-15
0000022020-08-25

 

First, we add the row of PurchaseDateNo. to the table and number each UserID and PurchaseDate as below.

UserIDPurchaseDatePurchaseDateNo.
0000012020-07-011
0000012020-07-122
0000012020-08-053
0000012020-08-214
0000022020-07-091
0000022020-08-062
0000022020-08-153
0000022020-08-254

 

Then, if we filter the line of July dates from each UserID and leave August dates as it is,
we also like to renumber each UserID and PurchaseDate like as below.

UserIDPurchaseDatePurchaseDateNo.
0000012020-08-051
0000012020-08-212
0000022020-08-061
0000022020-08-152
0000022020-08-253

 

However, we could not renumber correctly after changing the PurchaseDate.
Your advise and any solution would be highly appreciated.

 

Thanks.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Smitu30 

If you want to have a dynamic sort, you need to build a measure and use Rankx function. 

I build a table like yours to have a test.

1.png

Measure:

 

Measure = RANKX(FILTER(ALLSELECTED('Table'),'Table'[UserID]=MAX('Table'[UserID])),CALCULATE(MAX('Table'[PurchaseDate])),,ASC)

 

Result is as below.

Default:

2.png

Filter Date>=2020/8/1:

4.png

The max function in filter will return to a value = value in current row. So your filter will return to a table which values are all equal to current row.

Ex: Filter(Table,'Table'[UserID]=MAX('Table'[UserID])), IF current row's UserID = 1,so it will return to the table as below and calculate the rank of 'Table'[PurchaseDate] in this table. And you can refer to the blog as below to learn more about context filter and row filter.

3.png

For more info:

Filter 

row-context-and-filter-context-in-dax

You can download the pbix file from this link: How to dynamically number UserID and PurchaseDate

 

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

5 REPLIES 5
amitchandak
Super User
Super User

@Smitu30 , Try a measure like

 

calculate(distinctcount(Table[PurchaseDate]), filter( allselected(Table),Table[UserID] =max(Table[UserID]) && Table[PurchaseDate] <=max(Table[PurchaseDate])))

or

calculate(count(Table[PurchaseDate]), filter( allselected(Table),Table[UserID] =max(Table[UserID]) && Table[PurchaseDate] <=max(Table[PurchaseDate])))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hello.

 

Thank you very much for your advice.
I could number successfully after trying the measure.
 
Could you let me ask you one more thing about DAX as actually I am a beginner in it.
How did it come about that you put the below 2 conditions to FILTER function in this case.
 
「Table[UserID]=max(Table[UserID])」
Table[PurchaseDate]<=max(Table[PurchaseDate])」
 
Appreciate your further cooperation in advance.
Anonymous
Not applicable

Hi @Smitu30 

If you want to have a dynamic sort, you need to build a measure and use Rankx function. 

I build a table like yours to have a test.

1.png

Measure:

 

Measure = RANKX(FILTER(ALLSELECTED('Table'),'Table'[UserID]=MAX('Table'[UserID])),CALCULATE(MAX('Table'[PurchaseDate])),,ASC)

 

Result is as below.

Default:

2.png

Filter Date>=2020/8/1:

4.png

The max function in filter will return to a value = value in current row. So your filter will return to a table which values are all equal to current row.

Ex: Filter(Table,'Table'[UserID]=MAX('Table'[UserID])), IF current row's UserID = 1,so it will return to the table as below and calculate the rank of 'Table'[PurchaseDate] in this table. And you can refer to the blog as below to learn more about context filter and row filter.

3.png

For more info:

Filter 

row-context-and-filter-context-in-dax

You can download the pbix file from this link: How to dynamically number UserID and PurchaseDate

 

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. 

 

 

 

Hello.

 

Now, I am all clear.

Many thanks for your kind advice and explanation.

Greg_Deckler
Community Champion
Community Champion

@Smitu30 Can you explain the logic of the renumbering to me? You have 2 1's. But, overall, seems like you need a RANX measure. 

https://community.powerbi.com/t5/Quick-Measures-Gallery/To-Bleep-with-RANKX/m-p/1042520#M452

 

Ranking date.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.