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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
IfaZ_1
Helper I
Helper I

Nested If statement for Duplicate and Unique ID

Hi, 

I have tried to create a custom column based on nested if statement. However I think it doesnt give me the outcome that I want. 

 

This is my formula:

if [Account ID]> 1 and Text.EndsWith([Sourcing Individual],"Dir") then "Direct Sourced"
else
if [Account ID]> 1 and Text.EndsWith([Sourcing Individual],"Lic") then "Licensee Shared"
else
if [Account ID]= 1 and Text.EndsWith([Sourcing Individual],"Dir") then "Direct Sourced"
else
if [Account ID] = 1 and Text.EndsWith([Sourcing Individual],"Lic") then "Licensee Only"
else null

 

Basically, if I have a unique Account ID and its Sourcing Individual ends with "Dir" it should be Direct Sourced if end with "Lic" should be Licensee only. 

if the Account ID has duplicate and its Sourcing Individual ends with "Dir" it should be Direct Sourced and if it ends with "Lic" should be Licensee Shared. 

 

Here is my sample file: https://we.tl/t-Kv0HabVlgQ

 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @IfaZ_1 ,

 

Try the following code:

 

let
    Source = Excel.Workbook(File.Contents("C:\Excel\If Statement_Acoount ID duplicate.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    chgTypes = Table.TransformColumnTypes(Table1_Table,{{"Account ID", Int64.Type}, {"Account Name", type text}, {"Sourcing Individual", type text}}),

// Relevant steps ----->
    groupAccountID = Table.Group(chgTypes, {"Account ID"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"data", each _, type table [Account ID=nullable number, Account Name=nullable text, Sourcing Individual=nullable text]}}),
    expandData = Table.ExpandTableColumn(groupAccountID, "data", {"Account Name", "Sourcing Individual"}, {"Account Name", "Sourcing Individual"}),
    addOutput =
        Table.AddColumn(
            expandData,
            "output",
            each let __suffix = Text.End([Sourcing Individual], 3) in
            if __suffix = "Dir" then "Drect Sourced"
            else if [Count] > 1 and __suffix = "Lic" then "Licensee Shared"
            else if [Count] = 1 and __suffix = "Lic" then "Licensee Only"
            else "Unmapped"
        )
        
in
    addOutput

 

 

The trick here is to use Group By & Count Rows to identify if an [Account ID] is unique or not.

 

It gives this output:

BA_Pete_0-1689328611574.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

6 REPLIES 6
Vijay_A_Verma
Super User
Super User

Use this in your custom column

[cnt=List.Count(List.Select(#"Changed Type"[Account ID], (x)=> x=[Account ID])),
result = if Text.EndsWith([Sourcing Individual],"Dir") then "Direct Sourced" 
else if Text.EndsWith([Sourcing Individual],"Lic")
then if cnt > 1 then "Licensee Shared" else "Licensee Only" else null][result]
BA_Pete
Super User
Super User

Hi @IfaZ_1 ,

 

Try the following code:

 

let
    Source = Excel.Workbook(File.Contents("C:\Excel\If Statement_Acoount ID duplicate.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    chgTypes = Table.TransformColumnTypes(Table1_Table,{{"Account ID", Int64.Type}, {"Account Name", type text}, {"Sourcing Individual", type text}}),

// Relevant steps ----->
    groupAccountID = Table.Group(chgTypes, {"Account ID"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"data", each _, type table [Account ID=nullable number, Account Name=nullable text, Sourcing Individual=nullable text]}}),
    expandData = Table.ExpandTableColumn(groupAccountID, "data", {"Account Name", "Sourcing Individual"}, {"Account Name", "Sourcing Individual"}),
    addOutput =
        Table.AddColumn(
            expandData,
            "output",
            each let __suffix = Text.End([Sourcing Individual], 3) in
            if __suffix = "Dir" then "Drect Sourced"
            else if [Count] > 1 and __suffix = "Lic" then "Licensee Shared"
            else if [Count] = 1 and __suffix = "Lic" then "Licensee Only"
            else "Unmapped"
        )
        
in
    addOutput

 

 

The trick here is to use Group By & Count Rows to identify if an [Account ID] is unique or not.

 

It gives this output:

BA_Pete_0-1689328611574.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




IfaZ_1_0-1689559710516.png

Hi Pete, thank you for replying. I tried to used your code however, it is showing me Token Eof expected. 

 

The code I provided was not to be entered into a custom column, it was a complete example query.

Copy all of the code I sent, then create a new blank query in Power Query. Open Advanced Editor and paste all of my code over the default code in there.

You can now see the full example query working with each step.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Thank youuuuuu so much!!! I got it already. 

 

Cool, glad it's working for you.

Don't forget to give a thumbs-up on any posts that helped you 👍

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.