Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
HxH
Helper II
Helper II

Date column not working if created in PBI, working if created in Power Query

Hi everyone, 
Today I found a strange behaviour in Power BI that I don't understand. It is now solved, but I'd like to know what has happened because I don't understand it. 

I was trying to slice data by Year, month and other date columns from a standard date table. I created the connection between the date column of my date table (which was correctly marked as date table) and the date column I had in my fact table ("closed date"), and it wouldn't work. Keep in mind that this date column was created copying a "datetime" column and changing the format to just "date". When I tried to slice by year or month, all my rows were associated with a BLANK month or year or yearmonth or whatever column from the date table I tried to slice my rows with. 

Then I went in Power Query and I tried to replicate the same behaviour. I copied my datetime column and I changed the format to Date only (removing time, as I had done in Power BI). I recreated the relationship with the date table and tried to slice by this column and everything then worked. It's now correctly splitting data by month and it doesn't associate all my rows with blank date values.

What could be causing this behaviour? Is there a difference between copying a column in Power BI and changing the format, and doing the same thing in Power Query? Does power query perform some kind of automatic "data cleaning" or whatever that could have removed some errors from my original column ? (For example, I read online that one of the possible problems in the date column in my fact table could have been spaces that were messing up the relationship, could PQ have automatically got rid of these spaces while PBI didn't? ) 

Thanks for any explanation 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I had this problem too a couple of weeks ago and while mine is custom, I designated it as a  date table.  Unfortunately I don't remember if I designated the date table as such before or after I had created the relationships.  I think it will allow you to create the relationships regardless of if you designate the table before or after you draw them.

 

As for why it doesn't work the way you described, I think it's because only the format was changed from datetime to date, not the actual data type.  I had the same thing happen to me a few weeks ago and I solved it in a similar way in PQ because I had to preserve the time component (I just created a custom column and used the Date.From function in PQ, but I was trying to be more explicit, your way obviously worked too).  If I'm reading your post right, you initially changed the format with DAX after the data loaded.  This is not explicitly changing the data type, you're only changing how you're displaying it.  I suspect that if you had tried actually changing the data type from datetime to date in the option above the display format option in the Modeling tab in the loaded data model, it would have worked too.  I don't know if that fits your requirements though, you may need to preserve the time component too.

 

After figuring it out, I tried to find some explanation of why it behaves that way but I really couldn't find much.  My best (somewhat) educated guess is that once you designate a date table, the relationship only truly "works" if the key column in the date table is the same data type as the column in the fact table.  I suspect it lets you create the relationship because of the date component in both values but doesn't truly recognize the relationship because the time component creates some ambiguity between the tables.  Your method of creating that extra column and explicitly changing it to a data type of date worked because that value is explicitly a date and can create that one-to-one relationship with respect to matching a specific date key.  Obviously in practice the relationship should be one-to-many, but I think that unless the date column in the fact table can match to a specific key value, it doesn't work.  If you had changed the type in the Modeling tab of the loaded model (not just the format), it would have accomplished the same thing because it actually goes back and changes the PQ script to reflect the new data type.

 

I think the behavior has to do with when you designate as a date table, you're only allowed to pick columns where the value is of date data type, not datetime.  My guess is that this establishes the column you pick as a primary key of sorts even if it's not technically the primary key of the table if you were getting it from a database for example.  For the relationships to work properly, I think the column in the fact table has to be of date data type too.

 

I have a feeling if you didn't explicitly mark it as a date table the relationship would work fine.  In my experience the only advantage marking a date table as such gives you is if you need to do some of the more advanced time intelligence calculations past the built in YTD, SAMEPERIODLASTYEAR, etc. functions.  Where I work we have a week based fiscal calendar that renders the built in time intelligence functions unusable for anything dealing with fiscal year calculations, so I am pretty much forced to designate a date table.  I don't actually mind, I just have to remember to create an extra column in my fact tables that contain the date only if I need to keep a column with the original datetime value for visualization purposes.

 

If you don't need to use a custom calendar or do any advanced time calculations, I think you can probably get away without designating a date table.  If all you need is something for a continuous axis in a visualization or use with slicers/filters, I'm pretty sure you could just create a date table that you use solely as a dimensional table, not mark it as a date table, and still be able to slice using a relationship between the date column in the date table and any datetime or date column in a fact table.

 

Hope this helps.  I'm not claiming it's the official explanation or anything, it's just my best guess.  If anyone else knows exactly why this happens, I'd be interested in hearing it too.

 

Andrew

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

I had this problem too a couple of weeks ago and while mine is custom, I designated it as a  date table.  Unfortunately I don't remember if I designated the date table as such before or after I had created the relationships.  I think it will allow you to create the relationships regardless of if you designate the table before or after you draw them.

 

As for why it doesn't work the way you described, I think it's because only the format was changed from datetime to date, not the actual data type.  I had the same thing happen to me a few weeks ago and I solved it in a similar way in PQ because I had to preserve the time component (I just created a custom column and used the Date.From function in PQ, but I was trying to be more explicit, your way obviously worked too).  If I'm reading your post right, you initially changed the format with DAX after the data loaded.  This is not explicitly changing the data type, you're only changing how you're displaying it.  I suspect that if you had tried actually changing the data type from datetime to date in the option above the display format option in the Modeling tab in the loaded data model, it would have worked too.  I don't know if that fits your requirements though, you may need to preserve the time component too.

 

After figuring it out, I tried to find some explanation of why it behaves that way but I really couldn't find much.  My best (somewhat) educated guess is that once you designate a date table, the relationship only truly "works" if the key column in the date table is the same data type as the column in the fact table.  I suspect it lets you create the relationship because of the date component in both values but doesn't truly recognize the relationship because the time component creates some ambiguity between the tables.  Your method of creating that extra column and explicitly changing it to a data type of date worked because that value is explicitly a date and can create that one-to-one relationship with respect to matching a specific date key.  Obviously in practice the relationship should be one-to-many, but I think that unless the date column in the fact table can match to a specific key value, it doesn't work.  If you had changed the type in the Modeling tab of the loaded model (not just the format), it would have accomplished the same thing because it actually goes back and changes the PQ script to reflect the new data type.

 

I think the behavior has to do with when you designate as a date table, you're only allowed to pick columns where the value is of date data type, not datetime.  My guess is that this establishes the column you pick as a primary key of sorts even if it's not technically the primary key of the table if you were getting it from a database for example.  For the relationships to work properly, I think the column in the fact table has to be of date data type too.

 

I have a feeling if you didn't explicitly mark it as a date table the relationship would work fine.  In my experience the only advantage marking a date table as such gives you is if you need to do some of the more advanced time intelligence calculations past the built in YTD, SAMEPERIODLASTYEAR, etc. functions.  Where I work we have a week based fiscal calendar that renders the built in time intelligence functions unusable for anything dealing with fiscal year calculations, so I am pretty much forced to designate a date table.  I don't actually mind, I just have to remember to create an extra column in my fact tables that contain the date only if I need to keep a column with the original datetime value for visualization purposes.

 

If you don't need to use a custom calendar or do any advanced time calculations, I think you can probably get away without designating a date table.  If all you need is something for a continuous axis in a visualization or use with slicers/filters, I'm pretty sure you could just create a date table that you use solely as a dimensional table, not mark it as a date table, and still be able to slice using a relationship between the date column in the date table and any datetime or date column in a fact table.

 

Hope this helps.  I'm not claiming it's the official explanation or anything, it's just my best guess.  If anyone else knows exactly why this happens, I'd be interested in hearing it too.

 

Andrew

I used you method to create another Date column [new date column] based on the column that is not working [messed date column].
So at PQ I clicked to create a personalized column and used the formula = Date.From([messed date column]) wich resulted in [new date column]

So after that, I closed the PQ, and I used [new date column] to at my X axis.
Thank you!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.