Forum Discussion

Smitu30's avatar
Smitu30
New Member
5 years ago
Solved

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.

  • Anonymous's avatar
    Anonymous
    5 years ago

    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.

    Measure:

     

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

     

    Result is as below.

    Default:

    Filter Date>=2020/8/1:

    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.

    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. 

     

     

     

5 Replies

  • 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])))

    • Smitu30's avatar
      Smitu30
      New Member

      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's avatar
        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.

        Measure:

         

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

         

        Result is as below.

        Default:

        Filter Date>=2020/8/1:

        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.

        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.