Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
As the title say, if i have a table with 3 rows, and i want to add a flag to the first two rows that meet a condition, how can i do that in M code?
Invoice ID | Type | Weight | Destination | Sale ID |
A32 | Invoice | 10 | F | 1 |
A14 | Credit note | -10 | F | 1 |
A33 | Invoice | 10 | D | 1 |
In this example I have a list of invoices with different details and values (this is just an mock up example, can't post the real table). Sometimes due to a mistake while completing an invoice they have to cancell the invoice creating a credit note. See example: first row is the created invoice with destination F, but the correct destination was D, so they first create a credit note that cancells the invoice, and then they create an invoice with the correct destination. Notice how the credit note has a negative numeric value to indicate it's cancelling the first one.
What I want to do is create a custom column using M code in power query that flags the first two rows so that i can then know which inoices have been cancelled, and then flags the third row which is the one that's correct.
Invoice ID | Type | Weight | Destination | Sale ID | Flag |
A32 | Invoice | 10 | F | 1 | N |
A14 | Credit note | -10 | F | 1 | N |
A33 | Invoice | 10 | D | 1 | Y |
Also I would like to add a Y flag if there's only one apparence of the row, meaning there was no cancellation asociated with that sale ID.
In Power Query, First you group by your dataset as shown below...
then, expand the rest column...
Add a custom column...
Flag = if [Invoice_ID_New] = [Invoice ID] then "Y" else "N"
After that remove Invoice_ID_N column.
You will get your required column Flag.
User | Count |
---|---|
76 | |
75 | |
46 | |
31 | |
27 |
User | Count |
---|---|
99 | |
91 | |
51 | |
48 | |
47 |