Forum Discussion

BBIUser's avatar
BBIUser
Helper IV
3 years ago

Custom column formula for a Power BI report

There is an existing Power BI Query which is used\source for a Power BI report.

Custom Column 

if [Session Admin]= "Not assigned Session" then 0 else [Quantity] * 95

 

How does the formula change if I have to consider date and month? say for example, 

if [Session Admin]= "Not assigned Session" then 0 else [Quantity] * (if [Date] > 6/30/2021 then 100 else 95).

It is throwing Error if I use [Date] > 6/30/2021.

 

I want to actually use the 'Date' column from the screenshot.

Also, not sure if I need to enter the 12:00:00 AM time in the formula that is displayed as part of the 'Date' column.

 

Appreciated your help!

Thanks!

8 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi BBIUser ,
    please try the following syntax instead:
    if [Session Admin]= "Not assigned Session" then 0 else [Quantity] * (if [Date] > #date(2012,6,30) then 100 else 95)

     

    • BBIUser's avatar
      BBIUser
      Helper IV

      ImkeF Mahesh0016 Thanks for your responses!

      I get the same error and the formula did not work

      if [Session Admin]= "Not assigned Session" then 0 else [Quantity] * (if [Date] > #date(2021,6,30) then 100 else 95)

      Or

      Table.AddColumn(#"Renamed Columns", "Amount", each

      if [Session Admin]= "Not assigned Session" then 0 else [Quantity] * (if [Date] > #date(2021,6,30) then 100 else 95) --> Here the only difference between Mahesh formula and mine is I have 'Renamed columns' instead of 'Changed Type'

       

      Expression.Error: The Date operation failed because the resulting value falls outside the range of allowed values.

       

      The error says outside the range but I have data unitl 11/30/2022 and all records displays error. Below are some screenshots. Is it somethign to do with date and time format???

       

           

       

      Please let me know if you need any more info.

      Thanks!

       

       

      • jennratten's avatar
        jennratten
        Super User

        Hello - it looks like your date column may have varying formats.  Please try this:

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtc30jcyMDJUMDSyMjAAIgVHXyUdpaDM5AyF8MScktQiIM8IhIGKdA0NlWJ1opUs9Q0tsenyyy9RSCwuzkzPS01RCE4FsvLzgMKmcO1GYO1GUO1GeC21gOgy0jWAWApzAFBdflJmnkIwUGEVkGcJV2eEpM4It3PM4BqMlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Session Admin" = _t, Quantity = _t, Column1 = _t]),
            #"Extracted Text Before Delimiter" = Table.TransformColumns(Source, {{"Date", each Text.BeforeDelimiter(_, " "), type text}}),
            #"Changed Type1" = Table.TransformColumnTypes(#"Extracted Text Before Delimiter",{{"Date", type date}, {"Quantity", Int64.Type}}),
            #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each 
        if [Session Admin] = "Not assigned Session" then 0 else 
        [Quantity] * ( if Date.From([Date]) > #date(2021,6,30) then 100 else 95 ) 
        )
        in
            #"Added Custom"