Forum Discussion
Need help on DAX - Leg Number
Hello All
Need your help.
From the below data I need to get the leg number of each invoice ID
For each Invoice ID first SegmentID should be leg 1 and therefore followed second segment ID should be Leg 2 and thereafter 3, 4, 5.....
| InvoiceDetailID | InvoiceID | SegmentID |
| 2322023 | 870678 | 2476640 |
| 2322209 | 870747 | 2476794 |
| 2322215 | 870747 | 2476797 |
| 2322336 | 870747 | 2476897 |
| 2322337 | 870747 | 2476898 |
| 2322338 | 870747 | 2476899 |
| 2322339 | 870747 | 2476900 |
| 2322224 | 870750 | 2476803 |
| 2322226 | 870750 | 2476804 |
| 2322358 | 870782 | 2476912 |
| 2322358 | 870782 | 2476913 |
| 2322358 | 870782 | 2476914 |
| 2322358 | 870782 | 2476915 |
| 2218357 | 835570 | 2374865 |
| 2218357 | 835570 | 2374866 |
| 2218364 | 835572 | 2374869 |
| 2218364 | 835572 | 2374870 |
| 2230088 | 839629 | 2386244 |
| 2322013 | 870673 | 2476633 |
| 2322017 | 870675 | 2476636 |
Result to be shown as below
| InvoiceDetailID | InvoiceID | SegmentID | LegNumber |
| 2322023 | 870678 | 2476640 | 1 |
| 2322209 | 870747 | 2476794 | 1 |
| 2322215 | 870747 | 2476797 | 2 |
| 2322336 | 870747 | 2476897 | 3 |
| 2322337 | 870747 | 2476898 | 4 |
| 2322338 | 870747 | 2476899 | 5 |
| 2322339 | 870747 | 2476900 | 6 |
| 2322224 | 870750 | 2476803 | 1 |
| 2322226 | 870750 | 2476804 | 2 |
| 2322358 | 870782 | 2476912 | 1 |
| 2322358 | 870782 | 2476913 | 2 |
| 2322358 | 870782 | 2476914 | 3 |
| 2322358 | 870782 | 2476915 | 4 |
| 2218357 | 835570 | 2374865 | 1 |
| 2218357 | 835570 | 2374866 | 2 |
| 2218364 | 835572 | 2374869 | 1 |
| 2218364 | 835572 | 2374870 | 2 |
| 2230088 | 839629 | 2386244 | 1 |
| 2322013 | 870673 | 2476633 | 1 |
| 2322017 | 870675 | 2476636 | 1 |
Hey gauravnarchal ,
check the following calculated column, that should make it:
LegNumber = VAR vInvoiveID = myTable[InvoiceID] VAR vSegmentID = myTable[SegmentID] RETURN CALCULATE( COUNTROWS(myTable), myTable[InvoiceID] = vInvoiveID && myTable[SegmentID] <= vSegmentID, ALL(myTable) )If you need any help please let me know.If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍Best regardsDenisBlog: WhatTheFact.biFollow me: twitter.com/DenSelimovicgauravnarchal maybe add a rank column:
Rank Column = RANKX ( FILTER( 'Table', 'Table'[InvoiceID] = EARLIER ( 'Table'[InvoiceID] ) ), 'Table'[SegmentID], , ASC )✨ Follow us on LinkedIn
Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!
⚡ Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.⚡
2 Replies
- selimovd
Most Valuable Professional
Hey gauravnarchal ,
check the following calculated column, that should make it:
LegNumber = VAR vInvoiveID = myTable[InvoiceID] VAR vSegmentID = myTable[SegmentID] RETURN CALCULATE( COUNTROWS(myTable), myTable[InvoiceID] = vInvoiveID && myTable[SegmentID] <= vSegmentID, ALL(myTable) )If you need any help please let me know.If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍Best regardsDenisBlog: WhatTheFact.biFollow me: twitter.com/DenSelimovic - parry2k
Super User
gauravnarchal maybe add a rank column:
Rank Column = RANKX ( FILTER( 'Table', 'Table'[InvoiceID] = EARLIER ( 'Table'[InvoiceID] ) ), 'Table'[SegmentID], , ASC )✨ Follow us on LinkedIn
Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!
⚡ Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.⚡