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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

M Query to pull only prior month data from HANA

Hi All,

I’m trying to query a HANA view for only data from the month prior to the current month.  So if the current run date is 12/14/22, I’m looking for data with date values 11/01/22 thru 11/30/22. This will be used to refresh a Power BI report each month.

 

There are two fields that will determine the data that meets the criteria:

               PrimaryApproval

               FinalApproval

I’m looking for the correct m query syntax for:

WHERE "PrimaryApproval " date = Previous Month OR (“FinalApproval " = Previous Month AND "PrimaryApproval " is blank).

 

What I expect to be returned is all PrimaryApproval with dates 11/01/22 thru 11/30/22, and also FinalApproval with dates 11/01/22 thru 11/30/22 where the PrimaryApproval is blank (not populated).

1 ACCEPTED SOLUTION
Anonymous
Not applicable

This is what worked for me:

WHERE

               (

                              (

                                             "PrimaryApproval" > Add_Months(Last_Day(current_date), -2)

                                             AND "PrimaryApproval" <= Add_Months(Last_Day(current_date), -1)

                              )

                              OR (

                                             (

                                                            "FinalApproval" > Add_Months(Last_Day(current_date), -2)

                                                            AND "FinalApproval" <= Add_Months(Last_Day(current_date), -1)

                                             ) 

                                             AND "PrimaryApproval" = '00000000'

                                  )

               )

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

This is what worked for me:

WHERE

               (

                              (

                                             "PrimaryApproval" > Add_Months(Last_Day(current_date), -2)

                                             AND "PrimaryApproval" <= Add_Months(Last_Day(current_date), -1)

                              )

                              OR (

                                             (

                                                            "FinalApproval" > Add_Months(Last_Day(current_date), -2)

                                                            AND "FinalApproval" <= Add_Months(Last_Day(current_date), -1)

                                             ) 

                                             AND "PrimaryApproval" = '00000000'

                                  )

               )

Anonymous
Not applicable

A little more information:

 

We are using the _SYS_BIC schema. I would expect the statement below to get the previous month, but again, the 'DateTime' is an issue.

Date.IsInPreviousMonth(Date.AddMonths(DateTime.FixedLocalNow(), -1))

Something like this? 

 

WHERE
    (
        "PrimaryApproval" >= Date.AddMonths(Date.EndOfMonth(DateTime.LocalNow()), -1)
        AND "PrimaryApproval" < DateTime.LocalNow()
    )
    OR (
        "FinalApproval" >= Date.AddMonths(Date.EndOfMonth(DateTime.LocalNow()), -1)
        AND "FinalApproval" < DateTime.LocalNow()
        AND "PrimaryApproval" = null
    )
Anonymous
Not applicable

Hi Robert!

 

Thanks for the quick response. Having an issue with "DATETIME". In my attempts before posting to this board, I thought I had a solution but got this same message as I get with your suggested code: 

Athletc1_0-1671119601441.png

Also, won't what you provided return data from 11/1/22 thru 12/14/22 if it was executed today?

 

No clue currently, sorry..

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.