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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
SteffenSchulze
New Member

DAX: Counting multiple values in different columsn

Hi,

I have a table with disinct list of entities. Each entitiy is represented by two more columns which each describe a year.

This is what the table basically looks like:

Entity          Year1          Year2

1                 2020           2020

2                 2021           2022

3                 2021           2021

...                 ...                 ...

 

I would now like to count how often a year apperas in the column "Year1" and how often a year appears in column "Year2" and display this in a new table that could look like this:

 

Year           Amount in "Year1"         Amount in "Year2"

2020              1                                          1

2021              2                                          1

2022              0                                          1

 

Really appreciate your help!

Thanks!

1 ACCEPTED SOLUTION
VahidDM
Super User
Super User

Hi @SteffenSchulze 

 

Try this code to add a new table with DAX:

calctables_formulabarempty

Table 2 =
VAR _A =
    VALUES ( 'Table'[Year1] )
VAR _B =
    EXCEPT ( VALUES ( 'Table'[Year2] ), _A )
VAR _C =
    SELECTCOLUMNS ( UNION ( _A, _B ), "Year", [Year1] )
RETURN
    ADDCOLUMNS (
        _C,
        "Year1",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER ( 'Table', 'Table'[Year1] = EARLIER ( [Year] ) )
            ) + 0,
        "Year2",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER ( 'Table', 'Table'[Year2] = EARLIER ( [Year] ) )
            ) + 0
    )

 

Output:

VahidDM_0-1641511814995.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

View solution in original post

4 REPLIES 4
VahidDM
Super User
Super User

Hi @SteffenSchulze 

 

Try this code to add a new table with DAX:

calctables_formulabarempty

Table 2 =
VAR _A =
    VALUES ( 'Table'[Year1] )
VAR _B =
    EXCEPT ( VALUES ( 'Table'[Year2] ), _A )
VAR _C =
    SELECTCOLUMNS ( UNION ( _A, _B ), "Year", [Year1] )
RETURN
    ADDCOLUMNS (
        _C,
        "Year1",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER ( 'Table', 'Table'[Year1] = EARLIER ( [Year] ) )
            ) + 0,
        "Year2",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER ( 'Table', 'Table'[Year2] = EARLIER ( [Year] ) )
            ) + 0
    )

 

Output:

VahidDM_0-1641511814995.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

smpa01
Super User
Super User

@SteffenSchulze  I would now like to count how often a year apperas in the column - do you have a seperate table for year?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Yes, I do have a separate table with year.

@SteffenSchulze  spare more deatils? Are those two tables related? How?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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