Forum Discussion

flyingfox's avatar
flyingfox
Regular Visitor
4 years ago
Solved

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 ...
  • goncalogeraldes's avatar
    goncalogeraldes
    4 years ago

    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"

     

    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:

    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