Forum Discussion

mb0307's avatar
mb0307
Responsive Resident
5 years ago
Solved

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

  • 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

3 Replies

  • Jimmy801's avatar
    Jimmy801
    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

     

    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's avatar
      mb0307
      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's avatar
        Jimmy801
        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