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
Anonymous
Not applicable

new message

Hi,

 

I have a date filter created in a spreadsheet which I am using to connect multiple data sets together through one date slicer in pbix. In the spreadsheet, the order, for example, goes:

1 2017

2 2017

3 2017

 

However in Power Query it recognises the values as:

01/01/2017

01/02/2017

01/03/2017

 

When visualising data, this therefore displays data on the first day of the month, and none of the of the other days within that month.

Is there a way to convert the column of dates within Power Query, maybe via text, to just display jan-2017, feb-2017 so it is a sum of the values as a whole for that month?

 

Is this possible?

 

Thanks

 

1 ACCEPTED SOLUTION
v-huizhn-msft
Employee
Employee

Hi @Anonymous,

I reproduce your scenario and get expected result. Please follow the steps below.

1. I create sample table in excel with Text format.

 

sample table in spreadsheetsample table in spreadsheet
2. Inport it to Power BI, it recognises the values as date type, please cancel the "Changed type" step in Power Query.

3. 
Using this structure, we could write a CHOOSE function named fnChoose_CustCode for our scenario as follows:

 

fnChoose_CustCode = (input) => let
values = {
{"1","Jan"},
{"2","Feb"},
{"3","Mar"},
{"4","Apr"},
{"5","May"},
{"6","Jun"},
{"7","Jul"},
{"8","Aug"},
{"9","Sep"},
{"10","Doc"},
{"11","Nov"},
{"12","Dec"},
{input,"Undefined"}
},
Result = List.First(List.Select(values, each _{0}=input)){1}
in
Result,

Click View –> Advanced Editor. Let’s paste in our code of function just before the Source = line.

4. Split the Date column into two parts, month and year, then use the New function to transfer month to Jan,Feb etc format. Then combine the Month and Year column by adding a custom colum. 

5. Finally, remove the other columns you don't need. Click "Apply" in Power Query. You will get right result as follows.

Exact resultExact result

The below is my Power Query Statement, please review it.

let
    
fnChoose_CustCode = (input) => let
values = {
{"1","Jan"},
{"2","Feb"},
{"3","Mar"},
{"4","Apr"},
{"5","May"},
{"6","Jun"},
{"7","Jul"},
{"8","Aug"},
{"9","Sep"},
{"10","Doc"},
{"11","Nov"},
{"12","Dec"},
{input,"Undefined"}
},
Result = List.First(List.Select(values, each _{0}=input)){1}
in
Result,


Source = Excel.Workbook(File.Contents("C:\Users\v-huizhn\Desktop\New Microsoft Excel Worksheet.xlsx"), null, true),
    Sheet3_Sheet = Source{[Item="Sheet3",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet3_Sheet, [PromoteAllScalars=true]),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Promoted Headers", "Date", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Date.1", "Date.2"}),
    #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Custom", each Text.Combine({fnChoose_CustCode([Date.1]),"-",[Date.2]},"")),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date.1", "Date.2"})
in
    #"Removed Columns"


Best Regards,
Angelia

View solution in original post

1 REPLY 1
v-huizhn-msft
Employee
Employee

Hi @Anonymous,

I reproduce your scenario and get expected result. Please follow the steps below.

1. I create sample table in excel with Text format.

 

sample table in spreadsheetsample table in spreadsheet
2. Inport it to Power BI, it recognises the values as date type, please cancel the "Changed type" step in Power Query.

3. 
Using this structure, we could write a CHOOSE function named fnChoose_CustCode for our scenario as follows:

 

fnChoose_CustCode = (input) => let
values = {
{"1","Jan"},
{"2","Feb"},
{"3","Mar"},
{"4","Apr"},
{"5","May"},
{"6","Jun"},
{"7","Jul"},
{"8","Aug"},
{"9","Sep"},
{"10","Doc"},
{"11","Nov"},
{"12","Dec"},
{input,"Undefined"}
},
Result = List.First(List.Select(values, each _{0}=input)){1}
in
Result,

Click View –> Advanced Editor. Let’s paste in our code of function just before the Source = line.

4. Split the Date column into two parts, month and year, then use the New function to transfer month to Jan,Feb etc format. Then combine the Month and Year column by adding a custom colum. 

5. Finally, remove the other columns you don't need. Click "Apply" in Power Query. You will get right result as follows.

Exact resultExact result

The below is my Power Query Statement, please review it.

let
    
fnChoose_CustCode = (input) => let
values = {
{"1","Jan"},
{"2","Feb"},
{"3","Mar"},
{"4","Apr"},
{"5","May"},
{"6","Jun"},
{"7","Jul"},
{"8","Aug"},
{"9","Sep"},
{"10","Doc"},
{"11","Nov"},
{"12","Dec"},
{input,"Undefined"}
},
Result = List.First(List.Select(values, each _{0}=input)){1}
in
Result,


Source = Excel.Workbook(File.Contents("C:\Users\v-huizhn\Desktop\New Microsoft Excel Worksheet.xlsx"), null, true),
    Sheet3_Sheet = Source{[Item="Sheet3",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet3_Sheet, [PromoteAllScalars=true]),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Promoted Headers", "Date", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Date.1", "Date.2"}),
    #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Custom", each Text.Combine({fnChoose_CustCode([Date.1]),"-",[Date.2]},"")),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date.1", "Date.2"})
in
    #"Removed Columns"


Best Regards,
Angelia

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.