Power Query - Change Type Dynamically

gelu

Board Regular
Joined
Sep 30, 2022
Messages
85
Office Version
  1. 2021
Platform
  1. Windows
Hello there,

I would appreciate your help on the following.

Detect data type dynamically.

I deal with changing file formats and tables sizes.
When detecting data types I would like to tell PQ to take whatever columns are there and deal with each of them accordingly (instead of hard coded column names as it does by default).

What I do:

1 - I load one file to PQ from folder (there is always just one file but it could be .txt .csv .xls .xlsx or whatever else).

2 - I keep generic column headings when combining the files (by selecting "Do not detect data types"

1706692535532.png



3 - In the example below the table has 9 columns. But next time it could have 3 or 25 ...


Rich (BB code):
let
    Source = Csv.Document(Parameter1,[Delimiter="#(tab)", Columns=9, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Change Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, _
    {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, _
    {"Column7", type text}, {"Column8", type text}, {"Column9", type text}})
in
    #"Change Type"


How to do this dynamically?
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
One way:

Power Query:
#"Change Type" = Table.TransformColumnTypes(Source, List.Transform(Table.ColumnNames(Source), each {_, type text}))
 
Upvote 0
Dear Rory,

Thank you for your answer. I should have said that what I need is that each column data type must be detected. Right now they all are marked as TEXT.
What needs to be is Dates where the column contains dates, numers where numbers and text where text.

Sorry for not being more to the point the first time.

G
 
Upvote 0
How did you apply it? Once you have a table, it doesn't matter how it got into PQ, it's just a table.
 
Upvote 0
1 - I tried this:

Power Query:
let
    Source = Csv.Document(Parameter1,[Delimiter="    ", Columns=9, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    Custom1 = TransformColumnTypesDynamically(#"Promoted Headers")
in
    Custom1

2- I invoked it thus:
 

Attachments

  • TransformCOlumnTypesDynamically_Rory.png
    TransformCOlumnTypesDynamically_Rory.png
    9.1 KB · Views: 7
Upvote 0
I can solve it in Excel after the table is loaded (But it woul be nicer in PQ)

Function 2 - Dynamic Data Type.xlsx
ABCDEFGHIJKLM
1AGENCYTRANSACTION_DATETRANSACTION_AMOUNTVENDOR_NAMEMCC_DESCRIPTIONIndex
2District of Columbia Public Schools3/31/202331.18AMZN MKTP USBOOK STORES20521District of Columbia Public Schools4501631.18AMZN MKTP USBOOK STORES20521
3District of Columbia Public Schools3/31/202365.44AMZN MKTP USBOOK STORES20522District of Columbia Public Schools4501665.44AMZN MKTP USBOOK STORES20522
4District of Columbia Public Schools3/31/2023676.14AMZN MKTP USBOOK STORES20523District of Columbia Public Schools45016676.14AMZN MKTP USBOOK STORES20523
5District of Columbia Public Schools3/31/2023-36.03AMZN MKTP USBOOK STORES20524District of Columbia Public Schools45016-36.03AMZN MKTP USBOOK STORES20524
6District of Columbia Public Schools3/31/202397.89AMZN MKTP USBOOK STORES20525District of Columbia Public Schools4501697.89AMZN MKTP USBOOK STORES20525
7District of Columbia Public Schools3/31/202354.55AMZN MKTP USBOOK STORES20526District of Columbia Public Schools4501654.55AMZN MKTP USBOOK STORES20526
8District of Columbia Public Schools3/31/202332.46AMZN MKTP USBOOK STORES20527District of Columbia Public Schools4501632.46AMZN MKTP USBOOK STORES20527
9District of Columbia Public Schools3/31/202370.45AMZN MKTP USBOOK STORES20528District of Columbia Public Schools4501670.45AMZN MKTP USBOOK STORES20528
10District of Columbia Public Schools3/31/2023221.25AMZN MKTP USBOOK STORES20529District of Columbia Public Schools45016221.25AMZN MKTP USBOOK STORES20529
11District of Columbia Public Schools3/31/2023101.04AMZN MKTP USBOOK STORES20530District of Columbia Public Schools45016101.04AMZN MKTP USBOOK STORES20530
Sheet1
Cell Formulas
RangeFormula
H2:M11H2=IFERROR(IFERROR(DATEVALUE(A2),--A2),A2)
 
Upvote 0
Ah, yes I see the problem. The data is all imported as text, so the column types are all text.
 
Upvote 0
Ah, yes I see the problem. The data is all imported as text, so the column types are all text.
Only the data imported from folder does not respond to the function, though.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top