Forum Discussion

jayasurya_prud's avatar
jayasurya_prud
Advocate III
3 years ago
Solved

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.

  • Anonymous's avatar
    Anonymous
    3 years ago

    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:

     

    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

5 Replies

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

    • jayasurya_prud's avatar
      jayasurya_prud
      Advocate III

      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.

       

      • lbendlin's avatar
        lbendlin
        Super User

        What would be the average for John? 97?

  • Anonymous's avatar
    Anonymous
    Not applicable

    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:

     

    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