Forum Discussion

MenalM's avatar
MenalM
New Member
1 year ago
Solved

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?

  • 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

     

     

     Stéphane

5 Replies

  • 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

     

     

     Stéphane

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    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.

  • 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.

    • m_dekorte's avatar
      m_dekorte
      Icon for Resident Rockstar rankResident Rockstar

      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

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

       

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

      Done.

       

      I hope this is helpful

       

  • 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