Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

DAX Formula request: Using PREVIOUSMONTH and IF functions to retrive conditional date

Hi All

 

I am trying to create a DAX formula with the PREVIOUSMONTH and IF functions however do not know how to do it/what it should look like. I am trying to create a column (Column C - Meeting Month) to retrive the previous month based on the inputs of another column (Column B - Approval Date), howver in the instance where "Column C - Meeting Month" would be January or July then the formula should adjust it to read December or June respectively - as no meetings are taking place for approvals in Jan or Jul.

Below is a table which I am hoping to create, Column C - Meeting Month being where the DAX formula will apply to:

 

Column A - ProjectColumn B - Approval DateColumn C - Meeting Month
X21 Febuary 2021December 2020
Y22 March 2021Febraury 2021
Z23 August 2021June 2021

 

I have looked at the basics of using PREVIOUSMONTh and IF functions but am unable to apply them to this scenario.

 

I would appreciate any help as I am new to PowerBi Desktop.

 

Thankyou πŸ™‚
Ank

  • Anonymous 

     

     

    Meeting Month =
    VAR _sldate =
        CALCULATE ( SELECTEDVALUE ( 'Dicision Register'[Latest Decision Due Date] ) )
    VAR _twomonthback =
        EDATE ( _sldate, -2 )
    VAR _onemonthback =
        EDATE ( _sldate, -1 )
    VAR _p1 =
        EOMONTH ( 'Dicision Register'[Latest Decision Due Date], -1 )
    VAR _p2 =
        EOMONTH ( 'Dicision Register'[Latest Decision Due Date], -2 )
    VAR _isbalnkprev =
        ISBLANK (
            COUNTROWS (
                FILTER (
                    'Dicision Register',
                    'Dicision Register'[Latest Decision Due Date] > _p2
                        && 'Dicision Register'[Latest Decision Due Date] <= _p1
                )
            )
        )
    VAR _result =
        IF ( _isbalnkprev, _twomonthback, _onemonthback )
    RETURN
        _result 

     

     



    Did I answer your question? Mark my post as a solution!
    Appreciate with a kudos
    πŸ™‚

9 Replies

  • nandukrishnavs's avatar
    nandukrishnavs
    Icon for Community Champion rankCommunity Champion

    Anonymous -

     

    Try this calculated column.

     

     

    Column C - Meeting Month =
    VAR _sldate =
        CALCULATE ( SELECTEDVALUE ( 'Table'[Column B - Approval Date] ) )
    VAR _twomonthback =
        EDATE ( _sldate, -2 )
    VAR _onemonthback =
        EDATE ( _sldate, -1 )
    VAR _isbalnkprev =
        ISBLANK ( PREVIOUSMONTH ( 'Table'[Column B - Approval Date] ) )
    VAR _result =
        IF (
            ISBLANK ( PREVIOUSMONTH ( 'Table'[Column B - Approval Date] ) ),
            _twomonthback,
            _onemonthback
        )
    RETURN
        _result

     

     

     

    Since this is a data preparation activity, you can try M query as well. 



    Did I answer your question? Mark my post as a solution!
    Appreciate with a kudos
    πŸ™‚

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for the solution πŸ™‚

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi there,

       

      Sorry to have untaked the solution, Unfortunately when i tried your solution i am gven the following error when i input the below reccomended formula into a calculated column (titles and names being adjusted from the intiial example):

      "A table of multiple values was supplied where a single value was expected."

       
      • Anonymous's avatar
        Anonymous
        Not applicable

        sorry here are the photos: