Forum Discussion
Custom column formula for a Power BI report
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)
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!
- jennratten3 years agoSuper 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"- BBIUser3 years agoHelper IV
jennratten Appreciate your response.
May be this screenshot should help to understand more.
The custom column is within a table's applied steps. So, the formula that you provided is not working in this custom column.
Example:
let Source = Table.FromRowsis changing to something like this with error
Thanks for your help!
- jennratten3 years agoSuper User
Hello -
Can you please try creating a new blank query and open the Advanced Editor (button on the ribbon). The replace the entire contents of the Advanced Editor with this script below? After you do this, in the formula bar, you will see the portion of the script below that is outlined in RED will be gone.
It will look like this:
Script to replace all contents in the Advanced Editor:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtc30jcyMDJUMDSyMjAAIgVHXyUdpaDM5AyF8MScktQiIM9IKVYnWslS39ASm1q//BKFxOLizPS81BSF4FQgKz8PKGwK1mQE1WSE1wILiFqg2bqGhiDZ/KTMPIVgoHQVkGeJJGuE20IzpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Session Admin" = _t, Quantity = _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"Explanation:
Based on the screensnip below that you provided, it appeared as though the date/time values in your column were inconsistently formatted, with some being date/time and some being a date only as yyyy-mm. Please let me know if this assumption is incorrect and if so, can you please provide more information about the date values shown in your screenship with the yyyy-mm values?
Based on this assumption, the script I provided actually includes three new steps. These can be consolidated but I left them separate so they could be easily understood.
The script begins with the date column being formatted as text. It is important to begin this way if the values in the column have varying formats.
A step is then added to extract the portion of text that appears before the space, which leaves only the date (from both formats) and omits the time.
The proper types are then applied.
The new column can then be added, using the transformed dates.