March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
The datasource is an Excel spreadsheet and contains a column 'Date'. however is formatted as text.This allows users to enter the date, or text N/A or leave blank.
eg
in powerbi , if I convert the column to Date format with the steps ..
select Transform data, select column, in Transform, select Detect Data Type
The result the 'dates'convert to Date format. However the N/A is converted to Error and blanks converted to Null
In PowerBI I need a formula that reads the string, and by the value if N/A, blank or date then converts string to date, and then works out the number of days from the date and today.
Do I need something like
btain the string, if not n/a or blank convert string to date?
calcualte the number of days from date and today?
TIA
Solved! Go to Solution.
Hi @dd88
Thanks for the reply from @mahenkj2 and @AnalyticsWizard, please allow me to provide another insight:
Here I create a set of sample data:
Then we can use the dax DATEVALUE() to convert the text to datetime.
So add a calculate column to works out the number of days from the date and today:
Datediff =
IF (
'Table'[Date] <> "N/A"
&& 'Table'[Date] <> BLANK (),
DATEDIFF ( DATEVALUE ( 'Table'[Date] ), TODAY (), DAY )
)
The result is as follow:
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @dd88
Thanks for the reply from @mahenkj2 and @AnalyticsWizard, please allow me to provide another insight:
Here I create a set of sample data:
Then we can use the dax DATEVALUE() to convert the text to datetime.
So add a calculate column to works out the number of days from the date and today:
Datediff =
IF (
'Table'[Date] <> "N/A"
&& 'Table'[Date] <> BLANK (),
DATEDIFF ( DATEVALUE ( 'Table'[Date] ), TODAY (), DAY )
)
The result is as follow:
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
@v-zhengdxu-msft
I used the dax and amended. The formula is great as it cleans up the N/A and blanks. however the days difference returned is incorrect.
In Excel, I performed the caculations of date different from TODAY. These are the results
ANNUAL EXPIRY - Copy | TodaysDate | DaysDiff | ||
10/05/2025 | 29/04/2024 | 11 | ||
8/01/2025 | 29/04/2024 | 254 | ||
23/10/2024 | 29/04/2024 | 177 |
HI @dd88 ,
For an easier reporting perspective and proper rule to keep one kind of data in one column, in Power query you may duplicate the column as 'Expiry date' with conditional column (if NA or blank then blank else PC EXPIRY). This column will be your proper date column which you can as well later connect to date table if needed.
Then you can further duplcate PC expiry column as conditional column with (if NA or blank then no expiry else expiry available). This column can be used in report as filter if needed.
Finally remove PC expiry column.
You may play with something like this. but this is a simple strategy which may help you in many times during power bi report development tasks.
Please check if this helps in your situation.
To calculate the number of days from the date in the ‘Date’ column to today in Power BI, while handling “N/A” and blank entries, you can use the following Power Query M code:
let Source = Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content], ChangedType = Table.TransformColumnTypes(Source,{{"Date", type text}}), CustomFunction = (dateText) => if dateText = "N/A" or dateText = "" then null else Duration.Days(DateTime.LocalNow() - DateTime.FromText(dateText)), AddedCustom = Table.AddColumn(ChangedType, "DaysFromToday", each CustomFunction([Date])) in AddedCustom
Replace "YourTableName" with the actual name of your table. This script transforms the ‘Date’ column to text, checks if the value is “N/A” or blank, and if not, converts the string to a date and calculates the difference in days from today. If the value is “N/A” or blank, it returns null. Remember to add this code in the Advanced Editor in Power Query.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
134 | |
91 | |
89 | |
64 | |
58 |
User | Count |
---|---|
201 | |
137 | |
107 | |
72 | |
68 |