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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Athletc1
Regular Visitor

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
Athletc1
Regular Visitor

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
Athletc1
Regular Visitor

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'

                                  )

               )

Athletc1
Regular Visitor

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
    )

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors