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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
dfisher373
New Member

Converting from Month Name and Year to Date

I am a complete newby at Power BI so please excuse the simplicity of this post.

 

I have a table as follows:

 

Name  |   Month    |   Year   |    Sales Volume

 

Joe         March         2018        5000

 

I am trying to measure sales by Month. In addition, I'd like to be able to tabulate quarterly sales, YTD, etc. How do I setup a new column that tabulates the date from the Month and Year columns so that I can use it properly in Power BI? It is end of month data but I do not have the specific end of month dates.

 

Also, can anyone recommend a good book or website for a beginning user of Power BI that also shows how to properly setup data in Excel to make it easy to analyze?

 

By the way, I tried to upload the Excel file to this post but was unable to find an attach button. How do I upload files when I submit a question?

 

Thank you!

 

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

 

To compute sales in different time periods, you must use Date and Time Intelligence functions such as SAMEPERIODLASTYEAR(), PREVIOUSMONTH(), DATESYTD() etc.  These functions work only when you have an actual date column in your source datasets.  So in your Sales data table, you will have to create this calculated column formula to generate a date from the Year and Month

 

=1*("1/"&Sales[Month]&"/"&Sales[Year])

 

Format this as a Date and give this column a title, say Date.  As a next step, create a Calendar table by using the following formula (Modellling > New Table)

 

=CALENDAR(MIN(Sales{date]),MAX(Sales{date]))

 

In the Calendar Table, create addition column for Month and Year by ising these calculated column formulas = YEAR(Calendar[Date]) and =FORMAT(Calendar[Date],"mmmm").

Now create a relatioship from the Date column of the Sales table to the Date column of the Calendar Table.  In any visual that you build, drag the year and Month from the Calendar Table.  Now you should be able to use measures such as SAMEPERIODLASTYEAR(), PREVIOUSMONTH(), DATESYTD().


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

10 REPLIES 10
Ashish_Mathur
Super User
Super User

Hi,

 

To compute sales in different time periods, you must use Date and Time Intelligence functions such as SAMEPERIODLASTYEAR(), PREVIOUSMONTH(), DATESYTD() etc.  These functions work only when you have an actual date column in your source datasets.  So in your Sales data table, you will have to create this calculated column formula to generate a date from the Year and Month

 

=1*("1/"&Sales[Month]&"/"&Sales[Year])

 

Format this as a Date and give this column a title, say Date.  As a next step, create a Calendar table by using the following formula (Modellling > New Table)

 

=CALENDAR(MIN(Sales{date]),MAX(Sales{date]))

 

In the Calendar Table, create addition column for Month and Year by ising these calculated column formulas = YEAR(Calendar[Date]) and =FORMAT(Calendar[Date],"mmmm").

Now create a relatioship from the Date column of the Sales table to the Date column of the Calendar Table.  In any visual that you build, drag the year and Month from the Calendar Table.  Now you should be able to use measures such as SAMEPERIODLASTYEAR(), PREVIOUSMONTH(), DATESYTD().


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ahish. if you could. pl. help look at this. Just rying to have a better understanding on the basics with the date/time calculations and transformations. Also, is there any resource available online wherein one can try out examples and learn more on analysis involving dates?

 

calendar.PNG

calendar-1.PNG

 

br,

//sansk

Hi,

The Date function will defeinitely not work because the second input into that function should be a number (not text).  The second one appears correct and should work.  Share the link from where i can download your PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish. Pls. find below the link. Hope this is completely secure. 

 

https://drive.google.com/file/d/1WuDWeh-_W6tuPFcbH96NbbpwWsTvNSJp/view?usp=sharing

 

Br,

//sansk

Hi,

Try this calculated column formula

Date = 1*("1/"&'date'[Month]&"/"&'date'[Year])

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thx. Ashish for your quick help.

 

If you could pls. help understand the reason for prefix as "1*("

Also , if you could. pls. help with the below expresions - if these will alo fetch the same resuts. 

Getting some errors on trying this. 

 

= Date.FromText([Month] & " 1," & Number.ToText([Year]))

 

= Table.AddColumn(Source, "Custom", each Date.FromText([Month] & " 1," & Number.ToText([Year])))

 

br,

//sank

Hi,

You are welcome.  The 1* converts the "Date formatted as text" into an actual date entry.  You may read up on the Date.FromText from here.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks so much for your help. Do you have any suggested reading material or tutorials for someone who is just getting started with Power BI?

 

Thanks!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Super User
Super User

Well, I would create columns like these:

 

MonthNum = SWITCH([Month],"January",1,"February",2,"March",3)

Date = EOMONTH(DATE([Year],[MonthNum],1),0)

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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