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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply

DAX - query help

Hi, I am working on a report with two tables and joining them with left join in sql

 

table  1

 

Hike_YearNameMarks
2017john90
2018B91
2014A92
2015B93
2014C94
2015D95
2018E96
2019F97
2014E98
2019F99
2015C100
2016D101
2014G102
2017H103
2014john104

 

table 2 

 

Joined YearNameMarks
2014A10
2014c11
2014E12
2014john13

 

when I join this table to gether by left join by on clause with year

 

 

Hike_YearName_1Marks_1Joined YearName_2Marks_2
2017john90   
2018B91   
2014A922014A10
2015B93   
2014C942014c11
2015D95   
2018E96   
2019F97   
2014E982014E12
2019F99   
2015C100   
2016D101   
2014G102   
2017H103   
2014john1042014john13

 

 

now i need to calculate the average of Marks_2 column in the joined table.

 

But I dont have information more than 2014 in thr table 2. 

The present main year of the calculation is 2017 in the table 1.

 

Since I dont have the information on table 2 of 2017, I am gonna use 2014 as proxy for 2017

 

So I need to create a dax to calculate the average of the Marks_2 column in dax where the names are same in 2014 and 2017. 

 

In the above joined table, I have only John as present in 2017 and 2014 after joining the table. 

How create the dax with the condition.

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @jayasurya_prud ,

 

Here are the steps you can follow:

1. Create calculated column.

Avg =
var _select=SELECTCOLUMNS(FILTER(ALL(Table1) ,'Table1'[Hike_Year]=2017),"Name",[Name])
return
AVERAGEX(
    FILTER(ALL(Table2),
    'Table2'[Name] in _select &&
    'Table2'[Name]=EARLIER('Table1'[Name])&&
    'Table1'[Hike_Year]=2017),[Marks])

2. Result:

vyangliumsft_0-1670824167693.png

 

Best Regards,

Liu Yang

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

View solution in original post

5 REPLIES 5
v-yangliu-msft
Community Support
Community Support

Hi  @jayasurya_prud ,

 

Here are the steps you can follow:

1. Create calculated column.

Avg =
var _select=SELECTCOLUMNS(FILTER(ALL(Table1) ,'Table1'[Hike_Year]=2017),"Name",[Name])
return
AVERAGEX(
    FILTER(ALL(Table2),
    'Table2'[Name] in _select &&
    'Table2'[Name]=EARLIER('Table1'[Name])&&
    'Table1'[Hike_Year]=2017),[Marks])

2. Result:

vyangliumsft_0-1670824167693.png

 

Best Regards,

Liu Yang

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

lbendlin
Super User
Super User

So you want to use prior results as a proxy if data is missing?  Please show the expected outcome based on your sample data.

Thank you for the response. 

So in the above the joined table, for the column Joined Year, I only have 2014. No data after that. So What I am planning to do is calculate the average of the names which are present in both Joined year 2014 and Hike Year 2017 - Which is, in the table, I have only one name which is present in both records - It is John. 

So my dax should get an average from john. 

As this is the sample/dummy data, I am having only one name, But I may have a lot of names in the real data. So the average should be calulated in the Joined_Year column only when the names are similarly / present for both 2014 in Joined Year column and 2017 in Hike Year col. 

Hope this clarifies.

 

What would be the average for John? 97?

No. In marks 2 column it is 13 right, so my avg should come only 13. 

 

Likewise, I need to calculate the average of marks _2 column, only for the names present both in 2014 in joined year, name_2 and 2017 in Hike year and name column. 

 

So in the above table, only John present in hike year 2017 and 2014 in joined year. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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