Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
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
Solved! Go to Solution.
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
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
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
Hey,
so what you are saying is this:
Is my understaning of your requirement correct?
Regards
Hi TomMartens,
Yes. but we need to know the related contract based on the new and the old ID.
I have many customers that change the house and this example I just showing 2 customers.
Thus, how to write the M or Dax code to generate column " New"
Input
Old ID | New ID | Customer | Item | Status | Revenue | New |
20 | CustomerA | House No1 | Inactive | 5000 | 30 | |
20 | 25 | CustomerA | House No2 | Inactive | 10000 | 30 |
25 | 30 | CustomerA | House No3 | Active | 20000 | 30 |
60 | CustomerB | House NoB | Inactive | 20000 | 80 | |
60 | 65 | CustomerB | House NoC | Inactive | 10000 | 80 |
65 | 80 | CustomerB | House NoD | Active | 30000 | 80 |
Output:
Old ID | New ID | Customer | Item | Status | Revenue | New |
30 | CustomerA | House No3 | Active | 35000 | 30 | |
65 | 80 | CustomerB | House NoD | Active | 60000 | 80 |
Sorry for any inconvenince cause for my bad writing
Many thanks,
Regards,
Heng
Hey,
here is a little Power BI file I created three measures (should work the same in Power Pivot)
This measure identifies the last New ID (the status is active)
New = IF(HASONEVALUE('Table1'[Customer]) ,CALCULATE( FIRSTNONBLANK('Table1'[New ID], 1) ,ALLEXCEPT('Table1',Table1[Customer]) ,'Table1'[Status] = "Active" ) ,BLANK() )
I created measures to determine the corresponding Old ID and New ID where the Status is active
Old ID calc = IF(HASONEVALUE(Table1[Customer]), MAXX(VALUES('Table1'[Old ID]), IF(MAX('Table1'[New ID]) = [New] ,CALCULATE( MAX('Table1'[Old ID]) ,ALLEXCEPT('Table1',Table1[Customer]) ) ,BLANK() ) ), BLANK() )
and
New ID calc = IF(HASONEVALUE(Table1[Customer]), MAXX(VALUES('Table1'[New ID]), IF(MAX('Table1'[New ID]) = [New] ,CALCULATE( MAX('Table1'[New ID]) ,ALLEXCEPT('Table1',Table1[Customer]) ) ,BLANK() ) ), BLANK() )
and finally a measure that adds up all the revenues from each customer
Revenue total = CALCULATE( SUMX(VALUES('Table1'[Customer]), CALCULATE( SUM(Table1[Revenue]) ,ALLEXCEPT('Table1',Table1[Customer]) ) ) )
These measures create the following output
Hope this is what you are looking for
Regards
Hi TomMartens,
Thanks for your valuable time helping me.
Your measure is already working for customer but if I put the item it shows below result
but if I want to show the same output as my previous email
Output:
Old ID | New ID | Customer | Item | Status | Revenue | New |
25 | 30 | CustomerA | House No3 | Active | 35000 | 30 |
65 | 80 | CustomerB | House NoD | Active | 60000 | 80 |
Many thanks again
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
Hi Martens,
Now it is working. Thank you
Have a nice day!
Regards,
Heng
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
114 | |
100 | |
88 | |
69 | |
61 |
User | Count |
---|---|
151 | |
120 | |
103 | |
87 | |
68 |