Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
MenalM
New Member

Conditional Index Column in Power Query

Hi, 

 

I am struggling with an issue and need your help. I cannot find any solution.

 

I need to create an index column with specific requirements. My dataset includes several columns, among them Client, Contract, User, and Equipment. For each client, I may have multiple contracts; for each contract, there may be multiple users; and for each user, there may be multiple equipments. What I need is to have an index column that counts how many contracts I have for each client.

 

Specifically, the index should start at 1 for each new client, remain the same if the contract doesn't change, and increment if there is a new contract within the same client.

 

Could you please help me with this?

1 ACCEPTED SOLUTION
slorin
Super User
Super User

Hi, @MenalM 

 

let
Source = Your_Source,
Group = Table.Group(
Source,
{"Client"},
{{"Data1",
each Table.AddIndexColumn(Table.Group(_, {"Contract"}, {{"Data2", each _}}), "Index", 1)
}}),
Expand1 = Table.ExpandTableColumn(Group, "Data1", {"Data2", "Index"}, {"Data2", "Index"}),
Expand2 = Table.ExpandTableColumn(Expand1, "Data2", {"Contract"}, {"Contract"})
in
Expand2

2024-10-15 173553.png

 

 

 Stéphane

View solution in original post

5 REPLIES 5
MenalM
New Member

Hi @Greg_Deckler , 

Thanks for your answer, but that's not what I need.

 

What I need is to reset the index for each new client, keep the same index within the same client if the contract doesn't change, and increment it if the contract changes.

 

It should work something like this:



ClientContIndexCE MFGUtilisateurSerie
1108110444616P0010782YB50367U536837V
1108110444616S0017325CD4039D422219
42100110266618P000590611836559
42100110266618P00059091025
670000031140819P00103541EZ17046
670000032140820P0010354536103381
SK000008110228719P0010262535101200
SK000008110228719P00102625,36102E+13
SK000008110228719P001026263785.
SK000008110228719P001026282433
SK0000082123577S00208205306202480
SK0000082123577S0020820CD4045B189216

 

 

slorin
Super User
Super User

Hi, @MenalM 

 

let
Source = Your_Source,
Group = Table.Group(
Source,
{"Client"},
{{"Data1",
each Table.AddIndexColumn(Table.Group(_, {"Contract"}, {{"Data2", each _}}), "Index", 1)
}}),
Expand1 = Table.ExpandTableColumn(Group, "Data1", {"Data2", "Index"}, {"Data2", "Index"}),
Expand2 = Table.ExpandTableColumn(Expand1, "Data2", {"Contract"}, {"Contract"})
in
Expand2

2024-10-15 173553.png

 

 

 Stéphane

MenalM
New Member

Thanks for your answer. 

This is an exemple of my input data : 

ClientContract
Cl 1Cr 1
Cl 1Cr 1
Cl 2Cr 2
Cl 3Cr 3
Cl 3Cr 4
Cl 4Cr 5
Cl 5Cr 6

And my wanted output is : 

ClientIndexContract
Cl 11Cr 1
Cl 11Cr 1
Cl 21Cr 2
Cl 31Cr 3
Cl 32Cr 4
Cl 41Cr 5
Cl 51Cr 6



I hpe this is now more clear for you.

Hi @MenalM 

 

Follow these steps, make sure the Client column is selected

Choose Group By from the ribbon, set the operation to All Rows and and press OK

m_dekorte_0-1729005768766.png

Go to the Add Column tab and Add an Index Column from 1

m_dekorte_1-1729005808261.png

 

Final step is to expand missing fields, in this case Contract from the nested table.

Done.

m_dekorte_0-1729005956404.png

 

I hope this is helpful

 

Greg_Deckler
Super User
Super User

@MenalM Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.