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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
BeginnR
New Member

Trying to "secure" names of column-headers ...

Hello Everybody,

 

as I am new to this forum I'd like to introduce myself.

 

Twenty years ago (yes, I am above 50 ...), I was a developer und I used to code a lot with than C++, VB.net, ... so I am sort of familiar with Code-Structure. I thougt.

 

I am not that new to Power Query, using it here and there for about seven years for some simple tasks. A little bit of M is known to me but I am still a beginner. I have nothing but "ordinary" Power Query advanced editor in Excel, no Power BI Desktop, no Visual Studio.

 

Now I have the task to get data from files in a Folder (no problem so far), but people sending the data-files often change columen-headers by adding comments, hints, and so on.

 

Now I thougt why not trying to secure those entries by using a loop to compare a list of lists, containing a search-string and a name-string and then to rename the headers.

 

Thats my (not really working) code and I don't get my mistake(s):

 

let
Quelle = Folder.Files(Verzeichnispfad), // Verzeichnispfad is a parameter, containing the path

 

// Selecting the needed files

ExcelDateien=Table.SelectRows(Quelle, each [Extension] = ".xlsx" and not Text.StartsWith([Name], "~$") and Text.Contains([Name], "SearchString1")),

// Schleife durch alle Excel-Dateien im Verzeichnis
ErsetzteDateien = List.Transform(ExcelDateien[Content], each
let
// File-Loading
ExcelDatei = _,

 

// trying to extract date of deadline from file-name; not really working ...

Stichtag = Date.FromText(Text.BetweeenDelimiters(ExcelDatei[Name], "(Stichtag",")")),

 

// Loading the content
Inhalt = Excel.Workbook(ExcelDatei, true),

ErsteTabelle = Inhalt{[Item="Tabelle1", Kind="Table"]}[Data],

 

//Adding a column named "Stichtag" = Deadline - column added, but trying to fill in the date produces Error
TabelleMitStichtag = Table.AddColumn(ErsteTabelle, "Stichtag", each Stichtag, type date), // just trying; this works

 

// Looping throug Header-Names and replcing them, if a certain String is found

ErsetzteTabelle = List.Accumulate(L_Rename_SearchString1, TabelleMitStichtag, (tabelle, ersatz) =>
let
ZuErsetzenderName = ersatz{0}, // searchstring
NeuerName = ersatz{1}, //New Headername=constant name
// Trying to find the searchstring
ExistiertSpalte = List.Contains(Table.ColumnNames(tabelle), each Text.Contains(_, ZuErsetzenderName)),
// If SearchString is found, replace HeaderName with constant name
NeueTabelle = if ExistiertSpalte then Table.RenameColumns(tabelle, {{ZuErsetzenderName, NeuerName}}) else tabelle
in
NeueTabelle
)
in
ErsetzteTabelle
),

// creating a cross-table - just playing around ...
Endtabelle = Table.Combine(ErsetzteDateien)
in
Endtabelle

 

Well, I guess the code is full of errors and a way to help me, I imagine, could be a hint to a tutorial how to learn Coding in M - Language. Like, the problem is - there are so many tutorials out there - but a lot of them are a) related to the use of the GUI, b) not using the simple editor c) not about coding ...

 

Help with my code is also appreciated ;-).

 

Sorry for that terrible long post - if this is not the right place, feel free to move the thread.

 

Have a nice day

Rene

1 REPLY 1
lbendlin
Super User
Super User

Willkommen im Forum.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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