Forum Discussion

Barry_Kelly's avatar
Barry_Kelly
New Member
9 years ago
Solved

Date.IsInPreviousNDays M code question

Date.IsInPreviousNDays([Text Range], 2)

I'd like to use this function to dynamically query a fact table based on a slicer selection. Being unfamiliar with M, can another query, function or customized function be substitued for where the whole number goes, in this case 2.

Thanks



 

 

  • MarcelBeug's avatar
    MarcelBeug
    9 years ago

    You can either:

     

    Add 1 step to your first query: select the value, so the output of the query will be 1 value and you can replace the 2 in your function by <NameOfYourFirstQuery>.

     

    Or

     

    Replace the value 2 in your function by: Table.FirstValue(<NameOfYourFirstQuery>).

     

    I would prefer the first option.

     

12 Replies

  • dkay84_PowerBI's avatar
    dkay84_PowerBI
    Microsoft Employee

    I'm not sure I understand your objective.  Are you trying to replace the hard coded "2" with a dynamic value?

  • MarcelBeug's avatar
    MarcelBeug
    Community Champion

    Yes (in answer to the original question), provided the outcome is a value in the range of a 32-bit integer value.

    If the value is a negative integer, the result will always be false.

     

    Otherwise the first argument of the function should be a date/datetime or datetimezone field.

    [Text Range] doesn't sound like such.

     

    Edit: note that he comparison is always against the date of the last data refresh.

    • dkay84_PowerBI's avatar
      dkay84_PowerBI
      Microsoft Employee

      From M you can pass a parameter but not a slicer selection to replace the hard coded number of days (in addition to a query or formula that returns a number).

      • Barry_Kelly's avatar
        Barry_Kelly
        New Member

        Can you give me an example of what the code/function would look like that would be accepted by the Date.ISN.. function?

    • Barry_Kelly's avatar
      Barry_Kelly
      New Member

      [Text Range] is a column of dates in this example.

      Actually, I read some of your previous posts that led me to how I can Query the results from an Excel table that has an Excel slicer.
      Using the Aggregate function in Excel  it's possible to create a single row table in Power query based on the number of days a user selects.

      I'm trying to determine if its possible to plug that result into the M function.