October 15, 2024

Handling customers with same name

Spread the love

More often than not we face a situation where we have customer data with duplicate names. The differentiating column is something like a customer code. When we try to summarize our data using the customer name, Power BI aggregates the measures based on the customer name. So different customers (but with the same names) are aggregated resulting in an incorrect representation of the data.

Duplicate Names - 01

When we summarize the data by Customer Name we may be misled into believing that “Gregory Ramirez” has a 10,909 Sales Value but in reality, there are two customers with the same name. When we break our data by Code, we see that there in fact are two Gregory Ramirez with 7,442 and 3,467 Sales Value.

One solution is to concatenate the Code and Name column and use that instead.

Duplicate Names - 02

The problem with this approach is that the names get overly complicated and difficult to read, also we end up concatenating the names without any duplicate values.

Solution

The solution is to utilize a very powerful character from the Unicode set known as Zero-width space. It is a special character that affects how to print words used in some writing systems, we are not interested in their usage but the fact that they are invisible to the eye.

The idea is that if we add Zero-width space to the second Gregory Ramirez, then it will look the same to the human eye but for Power BI it will have two different names so it will not end up aggregating measures for them.

The first step is to identify the customer with the same name, rank them using the customer code, and then add spaces respectively. We will utilize DAX for this purpose.

Unique Name = 
VAR CustomersWithSameName =
    CALCULATETABLE (
        SUMMARIZE( Data, Data[Name], Data[Code] ),
        ALLEXCEPT ( Data, Data[Name] )
    )
VAR Ranking =
    RANKX ( CustomersWithSameName, Data[Code],, ASC, DENSE )
VAR Blanks =
    REPT ( UNICHAR ( 8204 ), Ranking - 1 )
VAR Result = Data[Name] & Blanks
RETURN
    Result

Let’s walk through the DAX

  • We first create a table with selected columns and store it in a variable “CustomersWithSameName” using SUMMARIZE DAX
    • The idea is to create a temporary table consisting only the columns which are required for our calculations and to remove any filters on them.
    • In our case, the table only consists of the three columns so we can simply refer to the table directly.
  • We then create another variable “Ranking” using RANKX DAX
    • The idea is to create assign a rank based on the code order.
  • Then we create another variable “Blanks” using REPT DAX
    • The idea behind this is to create the Zero-width characters based on the Rank.
    • For our calculation we use Ranking – 1
  • Finally we create and return the variable “Result” which is simply a concatenation of the Customer Name and Blanks

Let’s bring the new column into a table and see the result

Here, we can see that the names appear to be similar but Power BI is still breaking the measure and displaying it in the correct format.


You can find the Power BI file and the data used in the file on my GitHub repository here.


Spread the love

18 thoughts on “Handling customers with same name

  1. I?ve been exploring for a bit for any high-quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this web site. Reading this info So i am happy to convey that I’ve a very good uncanny feeling I discovered just what I needed. I most certainly will make sure to do not forget this site and give it a look on a constant basis.

  2. Spot on with this write-up, I absolutely feel this amazing site needs far more attention. I’ll probably be back again to read more, thanks for the information!

  3. I’d like to thank you for the efforts you’ve put in writing this blog. I am hoping to view the same high-grade content by you later on as well. In truth, your creative writing abilities has encouraged me to get my very own blog now 😉

  4. I truly love your blog.. Great colors & theme. Did you build this site yourself? Please reply back as I’m hoping to create my very own website and want to learn where you got this from or exactly what the theme is called. Cheers!

  5. Good website! I really love how it is simple on my eyes and the data are well written. I am wondering how I could be notified whenever a new post has been made. I’ve subscribed to your RSS which must do the trick! Have a nice day!

  6. Thanks for the sensible critique. Me & my neighbor were just preparing to do some research about this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such excellent info being shared freely out there.

Leave a Reply

Your email address will not be published. Required fields are marked *