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
mb0307
Responsive Resident
Responsive Resident

Last 6 months data

Hi,

 

Code below extract the data between two dates. 

 

 {Cube.ApplyParameter, "[!V000018]", {"[0DSDEL_DATE].[20200601]", "[0DSDEL_DATE].[20201130]"}}

 

Start date:   [0DSDEL_DATE].[20200601]

End date:    [0DSDEL_DATE].[20201130]

 

But I would like to make it dynamic becasue data will updated on daily basis.

 

I would like to extract last 6 months data but always starting from the 1st of first month.

 

Example: 

Current date:  02/12/2020

6 Months ago:  02/06/2020

So query should extract data between 01/06/2020 to 02/12/2020.  Please note that how the date range started from 1st of June not 2nd.

 

Thanks

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @mb0307 

 

here a code that creates two variables (CreateTextForQueryEnd  and CreateTextForQueryStart) that you can use in your code

let
    Today = Date.From(DateTime.FixedLocalNow()),
    YearToday = Text.From(Date.Year(Today)),
    MonthToday = "0"&Text.From(Date.Month(Today)),
    DayToday = "0"&Text.From(Date.Day(Today)),
    CreateTextForQueryEnd = "[0DSDEL_DATE].["&YearToday&Text.End(MonthToday,2)&Text.End(DayToday,2)&"]",
    Minus6MonthsFirstDayOfMonth = Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-6)),
    Year6Months = Text.From(Date.Year(Minus6MonthsFirstDayOfMonth)),
    Month6Months = "0"&Text.From(Date.Month(Minus6MonthsFirstDayOfMonth)),
    Day6Months = "0"&Text.From(Date.Day(Minus6MonthsFirstDayOfMonth)),
    CreateTextForQueryStart = "[0DSDEL_DATE].["&Year6Months&Text.End(Month6Months,2)&Text.End(Day6Months,2)&"]"
in
    CreateTextForQueryStart

 Copy paste this code to the advanced editor in a new blank query to see how the solution works. 


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

3 REPLIES 3
Jimmy801
Community Champion
Community Champion

Hello @mb0307 

 

you can calculate such a text-field to use it then in the cube-function. Here a example. However, I didn't understand which time frame you want to calculate. But if you check out the approach, you should have an idea how to continue

let
    FristDayOfThisMonth = Date.From(DateTime.FixedLocalNow()),
    Year = Text.From(Date.Year(FristDayOfThisMonth)),
    Month = "0"&Text.From(Date.Month(FristDayOfThisMonth)),
    Day = "0"&Text.From(Date.Day(FristDayOfThisMonth)),
    CreateTextForQuery = "[0DSDEL_DATE].["&Year&Text.End(Month,2)&Text.End(Day,2)&"]"
in
    CreateTextForQuery

Jimmy801_0-1606910333666.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

mb0307
Responsive Resident
Responsive Resident

@Jimmy801  Thanks for your response and apologies if my query wasn't clear.  I have edited my post with an example.  Your code doesn't go back 6 months and start from 1st of the oldest month.

Jimmy801
Community Champion
Community Champion

Hello @mb0307 

 

here a code that creates two variables (CreateTextForQueryEnd  and CreateTextForQueryStart) that you can use in your code

let
    Today = Date.From(DateTime.FixedLocalNow()),
    YearToday = Text.From(Date.Year(Today)),
    MonthToday = "0"&Text.From(Date.Month(Today)),
    DayToday = "0"&Text.From(Date.Day(Today)),
    CreateTextForQueryEnd = "[0DSDEL_DATE].["&YearToday&Text.End(MonthToday,2)&Text.End(DayToday,2)&"]",
    Minus6MonthsFirstDayOfMonth = Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-6)),
    Year6Months = Text.From(Date.Year(Minus6MonthsFirstDayOfMonth)),
    Month6Months = "0"&Text.From(Date.Month(Minus6MonthsFirstDayOfMonth)),
    Day6Months = "0"&Text.From(Date.Day(Minus6MonthsFirstDayOfMonth)),
    CreateTextForQueryStart = "[0DSDEL_DATE].["&Year6Months&Text.End(Month6Months,2)&Text.End(Day6Months,2)&"]"
in
    CreateTextForQueryStart

 Copy paste this code to the advanced editor in a new blank query to see how the solution works. 


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

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.

Top Solution Authors