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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Jules___
Frequent Visitor

DAX code to create calculated column based on multiple criteria

Hi I am struggling with creating a calculated colum to show some data  - i need it to be a calculated colum rather than a measure as I want to use the results in a map with conditional formatting.

 

here is the issue  I have the following data

 

locationquestion categorydatescore
londonsafety1/6/235
parisdata 1/2/240
londonsafety 1/2/242
madridfinance 1/2/245
londonfinance 1/2/242
madriddata1/6/235
paris safety1/6/235
parissafety 1/2/245
madriddata1/6/232
londonfinance 1/2/240

 

I want to create in a new table the summed score for each location for a particular category but i only want to use the data from the most recent date.

 

so far i have 

actual = VAR _location = 'table2'[location]
VAR _score =SELECTEDVALUE('table1'[location ])
VAR result =
CALCULATE(SUM('table1'[Score]), FILTER('table 1',table1'[category]="safety"), ALL('table 1'),
'table 1'[location]=_location
)

RETURN
result)
 
I just cannot work out how to also include date as a variable
 
many thanks for having a look
1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new table.

 

Jihwan_Kim_0-1708970531606.png

 

GROUPBY function (DAX) - DAX | Microsoft Learn

 

TREATAS function - DAX | Microsoft Learn

 

expected result table = 
VAR _condition =
    GROUPBY (
        data,
        data[location],
        data[question category],
        "@maxdate", MAXX ( CURRENTGROUP (), data[date] )
    )
VAR _t =
    CALCULATETABLE (
        data,
        TREATAS ( _condition, data[location], data[question category], data[date] )
    )
RETURN
    ADDCOLUMNS (
        SUMMARIZE ( _t, data[location], data[question category], data[date] ),
        "@ScoreSum", CALCULATE ( SUM ( data[score] ) )
    )

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
Jules___
Frequent Visitor

amazing this worked perfectly - thank you so much for your help

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new table.

 

Jihwan_Kim_0-1708970531606.png

 

GROUPBY function (DAX) - DAX | Microsoft Learn

 

TREATAS function - DAX | Microsoft Learn

 

expected result table = 
VAR _condition =
    GROUPBY (
        data,
        data[location],
        data[question category],
        "@maxdate", MAXX ( CURRENTGROUP (), data[date] )
    )
VAR _t =
    CALCULATETABLE (
        data,
        TREATAS ( _condition, data[location], data[question category], data[date] )
    )
RETURN
    ADDCOLUMNS (
        SUMMARIZE ( _t, data[location], data[question category], data[date] ),
        "@ScoreSum", CALCULATE ( SUM ( data[score] ) )
    )

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.