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
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
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