Forum Discussion

jlys89's avatar
jlys89
Frequent Visitor
5 years ago
Solved

PowerQuery Countifs across multiple columns

Hi 

 

I'm trying to figure out how to the do the following in PowerQuery (M) for Power BI where I want to add 2 additional columns to my data set as seen below:

What i want to achieve is counting the amount of times that a given feature is procent in the columns Feature 1 - Feature 6 where features are grouped into categories. (The 2 custom columns i need - Category 1 and Category 2)

 

Category 1: Feature 1, Feature 3, Feature 5

Category 2: Feature 2, Feature 4, Feature 6

 

For ID 1 the prefered method would be a count of: 3 because Feature 1, Feature 3 and Feature 5 is present in the 6 columns.

Afterwards I want to devide this by the total number of features meaning we get:

 

Category 1: 3/3 = 1,00 or 100 %

Category 2: 0/3 = 0  or 0 %

 

IDFeature 1Feature 2Feature 3Feature 4Feature 5Feature 6FeaturesCategory 1Category 2
1Feature 1nullFeature 3nullFeature 5null310
2nullnullFeature 3nullFeature 5Feature 630,6666666670,33
3nullnullFeature 3nullFeature 5Feature 630,6666666670,33
4nullFeature 2nullFeature 4nullnull200,67
5nullnullnullFeature 4nullnull10

0,33

 

Any help would be much appreciated 🙂

3 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    This is doable in query, but is much better done by unpivoting your data and then using a DAX measure.

     

    For example, here is a formula that will generate your Category 1 column in the query editor.  Put it in a custom column.

     

    = let thisrecordlist = Record.ToList(_) in List.Count(List.Select(List.Transform({1,3,5}, each thisrecordlist{_}), each Text.Contains(_, "Feature")))/3

     

    Regards,

    Pat

    • jlys89's avatar
      jlys89
      Frequent Visitor

      Hi Pat,
      Thanks alot of taking the time. Any chance you can explain how this part functions (List.Transform({1,3,5} I'm currently getting an error when utilizing your formula and I think it might be due to these columns being part of a bigger table.