Forum Discussion

mbug123's avatar
mbug123
Frequent Visitor
3 years ago

Current and LASTNONBLANK results

Hello,

 

I have this measure which gives me the percentage results for the currently filtered months:

 

overall_% = SELECTEDVALUE(a_fact_results[no_responses])/SELECTEDVALUE(a_fact_results[base])

I want to display the previous LASTNONBLANK results in a new column. How can I alter the code to display this? 
 
 

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Previous_LastNonBlank_Results = CALCULATE(
                                        LASTNONBLANK(
                                                     a_fact_results[no_responses], a_fact_results[month]
                                                                      ),
                                                                         FILTER(
                                                                                  a_fact_results,
                                                                                a_fact_results[month] < SELECTEDVALUE(a_fact_results[month])
                          )
    )

    • mbug123's avatar
      mbug123
      Frequent Visitor

      Many thanks for your code. Unfortunately, I got an error message because it was expecting a measure here for some reason

      Previous_LastNonBlank_Results = CALCULATE(
                                          LASTNONBLANK(
                                                       a_fact_results[no_responses], a_fact_results[month]
                                                                        ),
                                                                           FILTER(
                                                                                    a_fact_results,
                                                                                  a_fact_results[month] < SELECTEDVALUE(a_fact_results[month])
                            )
      )

  • 123abc's avatar
    123abc
    Icon for Community Champion rankCommunity Champion

    To display the previous LASTNONBLANK results in a new column, you can create a calculated column in your Power BI model. You'll need to use a combination of DAX functions to achieve this. Here's a modified version of your DAX measure that calculates the previous LASTNONBLANK result in a new column:

     

    previous_LASTNONBLANK_result =
    VAR CurrentMonth = MAX(a_fact_results[MonthColumn]) -- Replace 'MonthColumn' with the name of your date or month column
    VAR PreviousMonth = CALCULATE(MAX(a_fact_results[MonthColumn]), FILTER(ALL(a_fact_results), a_fact_results[MonthColumn] < CurrentMonth))
    RETURN
    IF(ISBLANK(PreviousMonth), BLANK(),
    CALCULATE(
    [overall_%],
    FILTER(a_fact_results, a_fact_results[MonthColumn] = PreviousMonth)
    )
    )

     

    In this code:

    1. VAR CurrentMonth retrieves the current month based on your filter context.
    2. VAR PreviousMonth calculates the previous month by finding the maximum month value that is less than the current month.
    3. The IF function checks if PreviousMonth is not blank. If it's blank, it returns blank. Otherwise, it calculates the [overall_%] measure for the previous month using the FILTER function to set the filter context to that month.

    Replace MonthColumn with the actual name of the column that represents your months or dates.

    You can create a new calculated column in your Power BI model and use this DAX formula. It will calculate the previous LASTNONBLANK result for each row based on the filter context of the current row.

     

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

    • mbug123's avatar
      mbug123
      Frequent Visitor

      Many thanks 123abc !

      I input your code and got an error message because 'PreviousMonth' is a reserved term. I changed it to 'ThePreviousMonth'. This gave me a response, unfortunately, all responses were 30 December 1899 (which I assume is the earliest date. 


      Would you be able to modify your code to make it give the correct answer, as I feel like its almost there? 

      surveydate column displays the full date of the last survey, e.g. 28/07/2019

      previous_LASTNONBLANK_result =
      VAR CurrentMonth =
          MAX ( a_fact_results[survey_date] )
      VAR ThePreviousMonth =
          CALCULATE (
              MAX ( a_fact_results[survey_date] ),
              FILTER ( ALL ( a_fact_results ), a_fact_results[survey_date] < CurrentMonth )
          )
      RETURN
          IF (
              ISBLANK ( ThePreviousMonth ),
              BLANK (),
              CALCULATE (
                  [overall_%],
                  FILTER ( a_fact_results, a_fact_results[survey_date] = ThePreviousMonth)
              )
          )