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
MarianneElver
Helper III
Helper III

Adding conditional unique index column based on several criterias

The challenge is the need for having a column listing an existing unique ID or create new unique ID based on the following criteria:

 

- If the customer no is recognized, then use the existing unique ID , that is previously tied up to this number

- If the customer no is not recognized, then look up the email , and if this email is recognized, then use the unique ID that is previously tied up to this number.

- If neither the customer no , nor email matches previous records, then lookup phone AND name . If they match, then use existing unique ID , otherways create new unique ID .

 

The condtional column with the unique ID can either be added in the existingtable, which is a XML file that will be updated occassionally( loaded into Power BI. All columns are expanded). Or, the unique ID can be created in a new, separate table. I have lowercased names and emails in separate columns to ensure match.

 

The purpose is to always have a column or table with unique index numbers tied to a customer name, so that statistics are based on unique clients.

 

Is this possible, and if yes, what language would be the best option to use - M or DAX?

 

Any kind of concrete suggestion is highly appreciated as I have been searching in vain for anything that matches this.

 

Thanks a lot in advance. Marianne

1 ACCEPTED SOLUTION

Thank you so much, looks very useful. I will try to implement this and get back with feedback as soon as possible. Thanks again!

View solution in original post

10 REPLIES 10
Greg_Deckler
Community Champion
Community Champion

M or DAX is going to depend, can you post some sample data and an idea of your various tables involved? Not even sure what your unique index column is, is it just a unique integer number?



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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi, thanks for yor quick reply! 

 

Sure, here is a little sample. The first four columns are currently provided and will receive new records when updated, and what I need is an additional column that, by checking for customer number or email, or name and phone, provides the ID that already exists, or creates a new unique interger number:

 

Customer NumberemailCustomer namePhoneID
10135000newname@gmail.comBenny Johnson43294636621
10031030mhy@du.comPete Madsen43461237462
10056030pete@gmail.comPete Madsen43461237462
10164001janet.nielsen@gmail.comDonna D. Nielsen43462722963
10258006sannypeterson9@gmail.comSanny Peterson43464146424
10258032sannypeterson9@gmail.comSanny Peterson43464146424

 

Hope that clarifies it, otherwise please let me know!

Thanks again,

Marianne

Anonymous
Not applicable

Hi @MarianneElver,

 

You can write a custom function to call the hash generate api, then input these condition columns to generate the unique code.

 

Regards,

Xiaoxin Sheng

Hi Xiaoxin, Thanks for the idea, unfortunately writing a function is a little ahead of my current capabilities where I'm having trouble finding the right M-code or DAX to figure it out how to come along this challenge! But thanks anyway, Marianne

Hi Marianne,

if my understanding of  your request is correct, then PowerBI isn't the right tool for it:

You're importing data in Power BI and want to create a value that sticks to the imported data, also when the next refresh is done?

Problem is, that there is currently no concept for storing such data in PBI. So you would need to export your new table with the newly generated ID's in order for them to be kept ... & reimported & merged with the new data (2nd run), where again new rows with empty ID's will come in.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi Imke, Thanks for your reply. I see, would data also not be stored if the generated IDs were fed/added into another table? I no, then is there any other method withing PBI for calculating the unique numbers of customers (and use this number in various other calculations) if the criteria for not counting the same customer twice are met? Thanks in advance, Marianne

Yes, you can create an Index-column like this, that will be recreated completely everytime you refresh your query like this:

 

let
    Source = Tabelle2,
    #"Grouped Rows" = Table.Group(Source, {"Phone"}, {{"All", each Table.AddIndexColumn(_, "NestedIndex",1,1), type table}}),
    TelIndex = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1),
    HasMatches = Table.AddColumn(TelIndex, "Custom", each List.Count(List.Union({[All][email], [All][Customer Number], [All][Customer name]})) <> List.Count(List.Distinct(List.Union({[All][email], [All][Customer Number], [All][Customer name]})))),
    #"Expanded All" = Table.ExpandTableColumn(HasMatches, "All", {"Customer name", "Customer Number", "email", "NestedIndex"}, {"Customer name", "Customer Number", "email", "NestedIndex"}),
    #"Added Custom" = Table.AddColumn(#"Expanded All", "NewIndex", each if [Custom] = false then Text.From([Index])&"-"& Text.From([NestedIndex]) else [Index]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"NestedIndex", "Index", "Custom"})
in
    #"Removed Columns1"

 

Just replace "Tabelle2" in the Source-step by the name of your query/table.

Question is what shall be returned, if there are more than 2 rows per telephone-number, and only 2 are matching an one is distinct. Using this, code, the non-matching row would also get the same ID. Not sure if this is what you want.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi, This IS actually what I am looking for! Will this be "stored forever" in Power BI alongside with the rest of the data? I think I will also change the grouping from group by Phone to Customer Number, to follow the Customer number-email-phone check in that order. Thanks a lot, Marianne

Great & yes, this will be stored and executed everytime you hit the refresh-button 🙂

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Thank you so much, looks very useful. I will try to implement this and get back with feedback as soon as possible. Thanks again!

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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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.