A Energy Question Customized Perform to Rename all Columns at As soon as in a Desk

Date:


A Power Query Custom Function to Rename all Columns at Once in a Table

I’m concerned with a Energy BI improvement previously few days. I obtained some information exported from numerous techniques in numerous codecs, together with Excel, CSV and OData. The CSV information are information export dumps from an ERP system. Working with ERP techniques may be very time consuming, particularly while you don’t have entry to the information mannequin, and also you get the information in uncooked format in CSV information. It’s difficult, as within the ERP techniques, the desk names and column names are usually not consumer pleasant in any respect, which is sensible. The ERP techniques are being utilized in numerous environments for a lot of totally different prospects with totally different necessities. So if we will get our fingers to the underlying information mannequin, we see configuration tables protecting column names. Among the columns are customized constructed to cowl particular wants. The tables might have many columns that aren’t essentially helpful for analytical functions. So it’s fairly important to have a superb understanding of the underlying entity mannequin. Anyhow, I don’t need to go off-topic.

The Drawback

So, right here is my situation. I obtained about 10 information, together with 15 tables. Some tables are fairly small, so I didn’t trouble. However a few of them are actually vast like having between 150 to 208 columns. Good!

Trying on the column names, they can’t be tougher to learn than they’re, and I’ve a number of tables like that. So I’ve to rename these columns to one thing extra readable, extra on this aspect of the story later.

Background

I emailed again to my buyer, asking for his or her assist. Fortunately they’ve a really good information knowledgeable who additionally understands their ERP system in addition to the underlying entity mannequin. I emailed him all the present column names and requested if he can present extra user-friendly names. He replied me again with a mapping desk in Excel. Right here is an instance to indicate the Column Names Mapping desk:

Column Names Mapping

I used to be fairly pleased with the mapping desk. Now, the following step is to rename all columns relies on the mapping desk. Ouch! I’ve nearly 800 columns to rename. That’s actually a ache within the neck, and it doesn’t sound fairly proper to burn the venture time to rename 800 columns.

However wait, what about writing automating the rename course of? Like writing a customized operate to rename all columns directly? I recall I learn a wonderful weblog submit about renaming a number of columns in Energy Question that Gilbert Quevauvilliers wrote in 2018. I positively advocate his weblog submit. So I need to do one thing much like what Gilbert did; making a customized operate that will get the unique columns names and brings again the brand new names. Then I exploit the customized operate in every desk to rename the columns. Straightforward!

The Answer

Earlier than we begin, I must have my mapping desk in Energy BI. So I create a brand new desk utilizing the Enter Knowledge performance. Then I copy the information from my mapping desk and paste it into the brand new desk. I title the brand new desk Column Names Mapping. The principle cause to make use of the Enter Knowledge performance as a substitute of getting information immediately from the file (in my case an Excel file) is that if for any cause sooner or later we miss the mapping file, the operate under can’t discover the columns to map. Therefore, it brings again the unique column names. This implies if we created a knowledge mannequin on prime of the brand new column names, then the entire mannequin breaks, which isn’t any good. I additionally disable load on the Column Names Mapping question as I don’t must have it in my information mannequin.

Creating fnRenameColumnsFromRefQuery Customized Perform

The operate reads by the Column Names Mapping desk and renames the columns of the question that we invoked the operate in when it finds the matching. If it doesn’t discover the matching, it leaves the column title is. Right here you go, that is the operate I got here up with.

//fnRenameColumnsFromRefQuery
(ColumnName as textual content) as textual content =>
let
    Supply = 
        if (
            Checklist.Comprises(
                File.FieldNames(#sections[Section1]), 
                "Column Names Mapping"
                ) 
             ) = true 
        then #"Column Names Mapping" 
        else null,
    ColumnNewName = 
        strive 
            if Checklist.Comprises(Supply[Column Name], ColumnName) = true 
            then 
                if Textual content.Trim(Desk.SelectRows(Supply, every ([Column Name] = ColumnName)){0}[Description]) = "" 
                then ColumnName 
                else Desk.SelectRows(Supply, every ([Column Name] = ColumnName)){0}[Description] 
            else Supply 
        in any other case ColumnName
in
    ColumnNewName

You possibly can obtain the previous expressions from right here.

Right here is the way it works:

  • The fnRenameColumnsFromRefQuery accepts a ColumnName parameter as textual content and the output of the operate is a textual content.
  • The Supply step checks the existence of a Column Names Mapping question. The Column Names Mapping is the mapping tables proven within the previous picture that holds the unique column names and their mapping. It’s possible you’ll use a unique title of alternative. If the Column Names Mapping question exists then Supply = #"Column Names Mappings" else Supply = null
  • The ColumnNewName step checks the contents of the Supply step which is both the Column Names Mapping desk or a null. If it’s the Column Names Mapping desk then it seems for the ColumnName parameter within the [Column Name] column inside the Column Names Mapping desk. If it finds the matching worth then it makes use of the ColumnName parameter to filters the [Column Name]. Then it will get the corresponding worth from the [Description] column which accommodates the brand new column title, in any other case it brings the unique column title again

The final step is to make use of the fnRenameColumnsFromRefQuery customized operate to rename the desk’s columns.

Invoking the fnRenameColumnsFromRefQuery Customized Perform

Energy Question has a operate to renamed column names in tables which is the Desk.TransformColumnNames(desk as desk, NameGenerator as operate) operate. The Desk.TransformColumnNames() operate accepts a NameGenerator as its second operand. So we will use our customized operate because the second operand inside the Desk.TransformColumnNames() operate to rename all columns. The ultimate code should appear to be this:

Desk.TransformColumnNames(PREVIOUS_STEP_NAME, fnRenameColumnsFromRefQuery)

Right here is an screenshot of the column names earlier than and after renaming the columns:

Renaming columns using a reference list in Power Query
Renaming columns utilizing a reference listing in Energy Question

You possibly can obtain the PBIX pattern right here.

Conclusion

If we predict what we’re doing is kind of laborious and we’re burning plenty of venture time doing that, it’s good to consider a strategy to automate the job. It would look a bit complicated, complicated and time-consuming at first, however consider me in lots of circumstances it price it and after you discover the way in which you need to use it in lots of different related situations which saves you plenty of time. Having mentioned that, there is no such thing as a assure that we will automate our situation, so we might be higher to be ready for it. For example, it’s a very long time that I’m considering to automate the method of renaming all queries showing within the Question Editor, however I’m but to discover a answer for it. Right here is the problem, in some circumstances, we hook up with the information supply, and we’ve got many tables to be loaded to the mannequin, and the desk names are usually not all the time consumer pleasant. It’s much like renaming columns however on the question stage. So in case you already know the answer, please share it with the neighborhood so everybody can profit out of your efforts. On the finish of the day, we be taught from one another. So sharing our information is a paying ahead to the following individual.

The opposite level is to all the time bear in mind to ask different consultants for assist. We is perhaps knowledgeable in some areas however it’s not possible for somebody being knowledgeable in all the things. Like in my case the shopper had an knowledgeable who supplied me the column names mapping desk, with out his assist I couldn’t get the job finished.

As all the time, I’d love know your opinion, so please share your ideas within the remark part under.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Share post:

Subscribe

spot_imgspot_img

Popular

More like this
Related

My First AI-Powered Meal? – Innovation Evangelism

First, an admission: I'm a horrible prepare dinner....

모든 CIO가 자문해야 할 ‘DX 질문’ 15가지

따라서 CIO는 과거보다 훨씬 빠른 속도로 이러한 지속적인...

along with InstaForex, heading for brand new victories! « Weblog InstaForex

InstaForex proudly proclaims that it's as soon as...

Digital Devoted Server: VPS vs Devoted Servers

If you happen to’re making an attempt to...