Forum Discussion

theo's avatar
theo
Helper III
5 years ago
Solved

add multiple columns like countif

Hi,

 

I have 2 questions and this may have been discussed elsewhere but I can't find it.

1. what is the dax required to count each entry - like a countif.  see example below, target is Count_RC.

 

IDRank codeCount_RC
10000AAA2
10001AAA2
10002AAB7
10003AAC1
10004AAE1
10005AAV1
10006AAB7
10007AAB7
10008AAB7
10009AAB7
10010AAB7
10011AAB7

 

2.  if I need to create multiple of these countifs, can I use the addcolumns to create multiple columns (around 1000 columns) rather than doing it one at a time?

target:

1 = ADDCOLUMNS('Filtering data',

            countifDax1,

            countifDax1

)

 

Thanks in advance.

  • theo's avatar
    theo
    5 years ago

    Hi v-janeyg-msft , thanks for the solution but as mentioned PQ's solution takes a while to complete for my data, so I modified the column expression mentioned initially to a measure instead since I found this tabular editor that can create multiple measures all at once.

     

    calculate(COUNTROWS(FILTER('28Jun_1973',[average diff]='28Jun_1973'[average diff])))

     

    Thanks for the help anyway.

10 Replies

  • selimovd's avatar
    selimovd
    Most Valuable Professional

    Hey theo ,

     

    in general you can do a kind of countif by just filtering.

     

    For your first question as example:

    Count_RC =
    VAR vRowRank = myTable[Rank]
    RETURN
        CALCULATE(
            COUNTROWS( myTable ),
            myTable[Rank] = vRowRank
        )

     

    Your second question I didn't really understand what you want to achieve. But you can add multiple criteria to your CALCULATE statement if that was the question.

     

    If you need any help please let me know.
    If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
     
    Best regards
    Denis
     
  • theo's avatar
    theo
    Helper III

    Hi selimovd for the quick reply.  Will try your advice and share the result.

    the second part is that I need this count_RC to be created for more columns, as rankCode is just one of them.
    since it is about 1000 columns, I am checking if there's a way that an addcolumns could add multiple countif columns .  

     

    I dont want to add column 1000x.

    • selimovd's avatar
      selimovd
      Most Valuable Professional

      Hey theo ,

       

      with a "normal" table that's not possible. The only way I know is to unpivot your data, so every of the 1000 columns would become a row with "columnName" and the second one "columnValue". Then you can filter for the name and a specific value, but you would also have to put the criteria in the measure.

       

      Where do you save the criteria? If it's in a table other approaches might work.

       

      If you need any help please let me know.
      If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
       
      Best regards
      Denis
       

       

      • theo's avatar
        theo
        Helper III

        My apology if not clear. 

        To clarify, my table has about 1200 columns of raw data. About 1000 columns need to have a calculated columns (COUNTS) which is the countif that I am looking for (see below example).

         

        IDRank codeAverage DiffsizeCount_RCAverage Diffsize
        10000AAA +01.00 to  +01.252.5255
        10001AAA +01.00 to  +01.252.5255
        10002AAB +01.00 to  +01.252.5155
        10003AAC +01.00 to  +01.252.5155
        10004AAE +01.00 to  +01.252.5155

         

        here is my failed attempt:

         

         

         

         

        addcolumns = ADDCOLUMNS('24Jun_1973',
        "count_average diff",COUNTROWS(FILTER('24Jun_1973',[average diff]=EARLIER([average diff]))),
        "count_rankCode",COUNTROWS(FILTER('24Jun_1973',[rankCode]=EARLIER([rankCode])))
        )

         

         

        but always have this error; multiple columns cannot be converted to scalar value

         

        I was able to create a countif DAX which is a bit faster than what you provided but thanks for the help.

         

         

        COUNTROWS(FILTER('24Jun_1973',[average diff]=EARLIER([average diff])))

         

         

        Can it be done in power query instead?