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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
flyingfox
Regular Visitor

Countif per alphanumeric Columns

Hi, I am new to PowerBI and am trying to figure out how to implement this scenario with DAX.

I have a table containing customers ID with some attributes and for each customer is associated a number of tickets (a ticket in a different cell):

  • Customer key
  • Customer description...
  • From Column n to Column (n+m) there is a Ticket number, starting with TT (e.g. TT001, TT099,etc)

For each row, it means for each customer, I would like to count the total number of Tickets.

I suppose to create a custom column containing the total number of  ticket, can anyone help me? In Excel I would have use a COUNTIF formula (filtering by LIKE "TT%"), it does not exist in Power BI. Thank you

1 ACCEPTED SOLUTION

Hello there @flyingfox ! Suppose you have a table like the one you shared, you can do the following:

 

1. Select the Customer and Company columns and right click on one of the two columns and select "Unpivot Other Columns"

goncalogeraldes_1-1645008148673.png

 

2. You can remove the "Attribute" column and rename the "Value" column

3. Close and apply

4. Use the following count measure:

 

Ticket p/ customer = COUNTA('Table 1 (Sheet1)'[Ticket])

 

 

The final result will be this:

goncalogeraldes_2-1645008436901.png

Here is the M code if you need it:

let
    Source = Excel.Workbook(File.Contents("C:\Users\Book1.xlsx"), null, false),
    Sheet1_sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    FilterNullAndWhitespace = each List.Select(_, each _ <> null and (not (_ is text) or 
    Text.Trim(_) <> "")),
    #"Removed Bottom Rows" = Table.RemoveLastN(Sheet1_sheet, each try 
    List.IsEmpty(List.Skip(FilterNullAndWhitespace(Record.FieldValues(_)), 1)) otherwise 
    false),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Bottom Rows", 
    [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Company ", 
    "Customer ID"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "Ticket"}})
in
    #"Renamed Columns"

Hope this answer solves your problem!
If you need any additional help please @ me in your reply.
If my reply provided you with a solution, please consider marking it as a solution ✔️ or giving it a kudoe 👍
Thanks!

You can also check out my LinkedIn!

Best regards,
Gonçalo Geraldes

View solution in original post

3 REPLIES 3
flyingfox
Regular Visitor

Thank you Gonçalo for your help, but probably I miss something because it does not work. Let's call the table CUSTOMER; Col1, Col2, Col3....Coln are the columns containing a Ticket id (starting with TT). I would like to count the total number of Ticket for each customer.   Thank you

Customer IDCompany Col1Col2Col3Col4Col5Col6......
ID1PaulTT001TT002TT005TT006TT021TT201 
ID2JohnTT304TT413TT521TT601   
ID3MarkTT114TT116     

 

Hello there @flyingfox ! Suppose you have a table like the one you shared, you can do the following:

 

1. Select the Customer and Company columns and right click on one of the two columns and select "Unpivot Other Columns"

goncalogeraldes_1-1645008148673.png

 

2. You can remove the "Attribute" column and rename the "Value" column

3. Close and apply

4. Use the following count measure:

 

Ticket p/ customer = COUNTA('Table 1 (Sheet1)'[Ticket])

 

 

The final result will be this:

goncalogeraldes_2-1645008436901.png

Here is the M code if you need it:

let
    Source = Excel.Workbook(File.Contents("C:\Users\Book1.xlsx"), null, false),
    Sheet1_sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    FilterNullAndWhitespace = each List.Select(_, each _ <> null and (not (_ is text) or 
    Text.Trim(_) <> "")),
    #"Removed Bottom Rows" = Table.RemoveLastN(Sheet1_sheet, each try 
    List.IsEmpty(List.Skip(FilterNullAndWhitespace(Record.FieldValues(_)), 1)) otherwise 
    false),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Bottom Rows", 
    [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Company ", 
    "Customer ID"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "Ticket"}})
in
    #"Renamed Columns"

Hope this answer solves your problem!
If you need any additional help please @ me in your reply.
If my reply provided you with a solution, please consider marking it as a solution ✔️ or giving it a kudoe 👍
Thanks!

You can also check out my LinkedIn!

Best regards,
Gonçalo Geraldes

goncalogeraldes
Super User
Super User

Hello there @flyingfox ! Check if this works for you:

CountIf =
CALCULATE (
    COUNTA ( 'Table'[Ticket Number] ),
    FILTER ( 'Table', LEFT ( 'Table'[Ticket Number], 2 ) = "TT" )
)

Hope this answer solves your problem!
If you need any additional help please @ me in your reply.
If my reply provided you with a solution, please consider marking it as a solution ✔️ or giving it a kudoe 👍
Thanks!

You can also check out my LinkedIn!

Best regards,
Gonçalo Geraldes

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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