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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Quiny_Harl
Resolver I
Resolver I

Concatenate month & year columns into an int type column and add a leading zero in front of month

Hi,

I have columns year and month of type int

YEARMONTH
20219
202110

I would like to combine them into a new column which need to be of type int as well and should have a leading zero in front of the month. Example: 202109

Note that I cannot create a text column and then change the type to int because that would screw up the query folding. 

 

Thanks!

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@Quiny_Harl  do you like to do this in server side or after you bring it over to PQ? 

 

in PQ this is how you can do it

Number.From(Text.From([Year])&(if(Text.Length(Text.From([Month]))=1) then Text.PadStart(Text.From([Month]),2,"0") else Text.From([Month])))

 

smpa01_0-1635961729885.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

7 REPLIES 7
smpa01
Super User
Super User

@Quiny_Harl  do you like to do this in server side or after you bring it over to PQ? 

 

in PQ this is how you can do it

Number.From(Text.From([Year])&(if(Text.Length(Text.From([Month]))=1) then Text.PadStart(Text.From([Month]),2,"0") else Text.From([Month])))

 

smpa01_0-1635961729885.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

@smpa01 

Thank you for your help. Actually, I accepted your answer as solution but then I saw that this solution prevents the query folding. Also, the resulting column is of type "any" and not not of type int. So unfortunatelly I cannot use it. 

@Quiny_Harl  which RDBMS are you using? You can do this transformation on the server side and not to be worried about query folding at all. ARe you using a TSQL?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

It's not possible to do it on the server side. Otherwise, life would have been much easier 🙂

@Quiny_Harl  like I asked what is the RDBMS?

 

and if it is TSQL, it is absolutely possible

declare @t1 as table (year int, month int)
insert into @t1
select * from
(values(2021,9),(2021,10)) t(a,b)

select *
, CASE WHEN LEN(month)=1 then convert(bigint,convert(nvarchar,year)+'0'+ convert(nvarchar,month))
       WHEN LEN(month)>1 then convert(bigint,convert(nvarchar,year)+ convert(nvarchar,month))
end as test
from @t1

 

smpa01_0-1635965241467.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Thank you for your suggestion but I don't have permission to edit the database. 

@Quiny_Harl  if it is TSQL, you don't need to have the database edit rights. This code will work on any table, any views. I created the table to demonstarte my point. Simply run the following portion

select *
, CASE WHEN LEN(month)=1 then convert(bigint,convert(nvarchar,year)+'0'+ convert(nvarchar,month))
       WHEN LEN(month)>1 then convert(bigint,convert(nvarchar,year)+ convert(nvarchar,month))
end as test
from transactionTBL

 

Writing a query on the database does not need edit right. Please check with youur DBA. As long as you can query the table from databse (as you are doing it now), you will not have issues running this native query.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

August Carousel

Fabric Community Update - August 2024

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

Top Solution Authors
Top Kudoed Authors