Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Add new column based on current row values, using multiple filters

Hi there

 

I am new to Power BI and I am still learning how to use DAX.

 

I want to add a calculated column which uses multiple filters based on a value taken from the current column. My purpose is to look up current months value and add a column with the last months value for that row (I would like to create the column in red below).

 

ValueIDTypeDateProject NameLast Month DateDates MonthDates LastMonthLast Month Value
0KPI 1Red01/09/2020Project A02/08/20202020092020081
0KPI 2Red01/09/2020Project A02/08/20202020092020082
0KPI 1Red01/09/2020Project B02/08/20202020092020083
0KPI 2Red01/09/2020Project C02/08/20202020092020084
1KPI 1Red10/08/2020Project A11/07/20202020082020070
2KPI 2Red10/08/2020Project A11/07/20202020082020070
3KPI 1Red10/08/2020Project B11/07/20202020082020070
4KPI 2Red10/08/2020Project C11/07/20202020082020070
0Task 1Blue28/08/2020Project A29/07/20202020082020071000
0Task 2Blue28/08/2020Project A29/07/20202020082020072000
0Task 3Blue28/08/2020Project A29/07/20202020082020073000
0Task 4Blue28/08/2020Project A29/07/20202020082020074000
1000Task 1Blue28/07/2020Project A28/06/20202020072020060
2000Task 2Blue28/07/2020Project A28/06/20202020072020060
3000Task 3Blue28/07/2020Project A28/06/20202020072020060
4000Task 4Blue28/07/2020Project A28/06/20202020072020060

 

I have copied the DAX on the link below as it is similar to my problem.

https://community.powerbi.com/t5/Desktop/Add-new-column-with-filters-based-on-current-row-values/m-p/1329206

 

Last Month Value =
VAR LastMonth = 'PBI Risk Project'[Dates.LastMonth]
RETURN
CALCULATE(
SUM('PBI Risk Project'[Value]),
FILTER(ALL('PBI Risk Project'),
'PBI Risk Project'[Dates.Month]=LastMonth ))

 

This does filter my data by the last month but it gives only one total number of the value column for the month on for each row. I need help to filter by Last Month, Project Name and ID. 

 

There is can be multiple IDs of the same name but only one per project per month e.g. Project A only has one KPI 1 for September, one in August and one in July , but there are multiple projects. Is it possible to filter by Last month AND, by Project Name AND by ID for the additional column ?

 

Many thanks in advance for your help

  • Here is a calculated column expression that returns your desired result.  FYI that you could also do this as a measure instead.

     

    Last Month Value New =
    VAR lastmonth = 'PBI Risk Project'[Dates LastMonth]
    RETURN
        CALCULATE (
            SUM ( 'PBI Risk Project'[Value] ),
            ALLEXCEPT (
                'PBI Risk Project',
                'PBI Risk Project'[Project Name],
                'PBI Risk Project'[ID]
            ),
            'PBI Risk Project'[Dates Month] = lastmonth
        ) + 0
    

     

    Regards,

    Pat

     

2 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Here is a calculated column expression that returns your desired result.  FYI that you could also do this as a measure instead.

     

    Last Month Value New =
    VAR lastmonth = 'PBI Risk Project'[Dates LastMonth]
    RETURN
        CALCULATE (
            SUM ( 'PBI Risk Project'[Value] ),
            ALLEXCEPT (
                'PBI Risk Project',
                'PBI Risk Project'[Project Name],
                'PBI Risk Project'[ID]
            ),
            'PBI Risk Project'[Dates Month] = lastmonth
        ) + 0
    

     

    Regards,

    Pat

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      It worked! Thank you very much for your help ğŸ™‚

       

      If I use a similar expression in a measure it will work as well or it needs to be written differently?