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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
theo
Helper III
Helper III

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.

1 ACCEPTED SOLUTION

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.

View solution in original post

10 REPLIES 10
theo
Helper III
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.

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
 

 

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?

 

Hi, @theo 

 

vjaneygmsft_0-1625818770931.png

Because addcolumn() function creates a table, you need to use the 'new table' feature.

 

PQ example count:

List.Count(Table.SelectRows(#"Changed Type",(x)=>x[name]=[name])[name])

vjaneygmsft_1-1625819471210.png

 

Best Regards

Janey Guo

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Thanks @v-janeyg-msft , however, when I try you solution for one column, it is not yet finished even after 10 minutes so I'm afraid it would be too long for the 1000 columns.

Whereas for this one, about 12 sec per column

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

 

Already tried the "new table" but same scalar error.

 

Appreciate any suggestion. thanks.

Hi, @theo 

 

In response to the information you gave, I can only respond like this. I also provided pq's solution, is there any problem? 

 

Best Regards

Janey Guo

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.

Hi, @theo 

 

If you can find a solution that meets your own conditions, I'm very happy. Can you kindly mark the helpful answer as a solution if you feel that makes sense?

 

Best Regards

Janey Guo

 

In relation to this, found some discussion on the use of the Tabular Editor.  However, I am not well versed on the script creation.  Would appreciate that can advise.

selimovd
Super User
Super User

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
 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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