Forum Discussion
Generate Unique ID in Power Query (M Code) or Power Power Pivot (DAX Code)
Hi Everyone,
Could anyone help me to write M or Dax Code to generate Unique ID as shown below
| Old ID | New ID | Item | Unique ID |
| 1 | ATN1 | 3 | |
| 1 | 2 | ATN2 | 3 |
| 2 | 3 | ATN3 | 3 |
Many thanks for your help.
Regards,
Heng
Hey,
just adjust the measure New like so
New = var New = IF(HASONEVALUE('Table1'[Customer]) ,CALCULATE( FIRSTNONBLANK('Table1'[New ID], 1) ,ALLEXCEPT('Table1',Table1[Customer]) ,'Table1'[Status] = "Active" ) ,BLANK() ) return IF(HASONEVALUE('Table1'[Customer]) ,IF(VALUES(Table1[New ID]) = New ,New ,BLANK() ) )and the measure Revenue total like so
Revenue total = var New = IF(HASONEVALUE('Table1'[Customer]) ,CALCULATE( FIRSTNONBLANK('Table1'[New ID], 1) ,ALLEXCEPT('Table1',Table1[Customer]) ,'Table1'[Status] = "Active" ) ,BLANK() ) return IF(HASONEVALUE('Table1'[Customer]) ,IF(VALUES(Table1[New ID]) = [New] ,CALCULATE( SUMX(VALUES('Table1'[Customer]), CALCULATE( SUM(Table1[Revenue]) ,ALLEXCEPT('Table1',Table1[Customer]) ) ) ) ,BLANK() ) ,CALCULATE( SUMX(VALUES('Table1'[Customer]), CALCULATE( SUM(Table1[Revenue]) ,ALLEXCEPT('Table1',Table1[Customer]) ) ) ) )Then the output will look like this
Regards
8 Replies
- TomMartensSuper User
Hey,
can you please describe the rule how the "Unique ID" has to be rerived, and also I do not understand what makes the ID unique if it repeats 3 times.
Regards
- SuyhengFrequent Visitor
Hi TomMartens,
For example, customer buy house No1 but later they change to House No2 and later on they change to House No3 but the revenue already paid in house No1 and House No 2. Thus I want to sum all the revenue for House No3 which I need to have the same ID in order for me to sum up the revenue
Input:
Old ID New ID Customer Item Status Revenue New 1 CustomerA House No1 Inactive 5000 3 1 2 CustomerA House No2 Inactive 10000 3 2 3 CustomerA House No3 Active 20000 3 Output: M or Dax code to show this result
Old ID New ID Customer Item Status Revenue New 2 3 CustomerA House No3 Active 35000 3 Many thanks for help
- TomMartensSuper User
Hey,
so what you are saying is this:
- the column "New" gets its value from the "latest" (maximum) value of column "New ID"
- create a measure "overall revenues" and just display the value if the latest "New ID" is selected
Is my understaning of your requirement correct?
Regards