cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
gtaranti Frequent Visitor
Frequent Visitor

Power Query Table.AddIndexColumn only for non-null elements

Hi all,

 

I have a problem with the followiing table :

 

Column1                  |  OTHER COLUMNS ....    |  
--------------------------------------------------|
QuestionX                |   other values ....    |
null                     |   other values ....    |
null                     |   other values ....    |
QuestionY                |   other values ...     |
null                     |   other values ....    |
null                     |   other values ....    |
null                     |   other values ....    |
QuestionZ                |   other values ...     |
...
..
..

 

I'm trying to add an Index Column but taking into account only the non-null elements of Column1.

i.e. the new column should have 1 in QuestionX, 2 in QuestionY, 3 in QuestionZ.

(QuestionX, QuestionY, etc are all arbitrary text. and the number of intermediate nulls is also random)

 

My ultimate goal is to rename the Questions in Column1 to Question1, Question2 e.t.c

 

Is there a  way to do that?

1 ACCEPTED SOLUTION

Accepted Solutions
gtaranti Frequent Visitor
Frequent Visitor

Re: Power Query Table.AddIndexColumn only for non-null elements

 

Thanks, but I'm not using PowerBI (maybe I used the wrong forum to post). 

I'm using only Excel with Power Query, trying to clean some data (exported responses from SurveyMonkey online forms)

So I don't know where to enter the DAX expression.

 

But finally, I came up with a solution with Power Query.

I'm building as a mid-step a translation table on-the-fly, and using it to get the final column like that :

 

 

    ...
InitialQuestionsList = List.RemoveNulls(Table.Column(Source, "Column1")), QuestionsListNumbers = List.Numbers(1, List.Count(InitialQuestionsList)), QuestionsList = List.Transform(QuestionsListNumbers, each "Q" & Text.From(_)), TranslationTable = Table.FromColumns({QuestionsListNumbers,InitialQuestionsList}), Final = List.Accumulate(Table.ToRows(TranslationTable), Source, (t,r) => Table.ReplaceValue(t, r{1}, r{0}, Replacer.ReplaceValue, Table.ColumnNames(t)))

 

I don't know if it the best/easiest solution, but it works.

3 REPLIES 3
Community Support Team
Community Support Team

Re: Power Query Table.AddIndexColumn only for non-null elements

@gtaranti,

 

You may follow the steps below.

1) add an index column in Query Editor

2) add a calculated column in DAX

Column =
IF (
    NOT ( ISBLANK ( Table1[Column1] ) ),
    RANKX (
        FILTER ( Table1, NOT ( ISBLANK ( Table1[Column1] ) ) ),
        Table1[Index],
        ,
        ASC,
        DENSE
    )
)
Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
gtaranti Frequent Visitor
Frequent Visitor

Re: Power Query Table.AddIndexColumn only for non-null elements

 

Thanks, but I'm not using PowerBI (maybe I used the wrong forum to post). 

I'm using only Excel with Power Query, trying to clean some data (exported responses from SurveyMonkey online forms)

So I don't know where to enter the DAX expression.

 

But finally, I came up with a solution with Power Query.

I'm building as a mid-step a translation table on-the-fly, and using it to get the final column like that :

 

 

    ...
InitialQuestionsList = List.RemoveNulls(Table.Column(Source, "Column1")), QuestionsListNumbers = List.Numbers(1, List.Count(InitialQuestionsList)), QuestionsList = List.Transform(QuestionsListNumbers, each "Q" & Text.From(_)), TranslationTable = Table.FromColumns({QuestionsListNumbers,InitialQuestionsList}), Final = List.Accumulate(Table.ToRows(TranslationTable), Source, (t,r) => Table.ReplaceValue(t, r{1}, r{0}, Replacer.ReplaceValue, Table.ColumnNames(t)))

 

I don't know if it the best/easiest solution, but it works.

Community Support Team
Community Support Team

Re: Power Query Table.AddIndexColumn only for non-null elements

@gtaranti,

 

Glad to hear that. You may help accept solution. Your contribution is highly appreciated.

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Power BI Helps Homeless and Trouble Youth

Power BI Helps Homeless and Trouble Youth

We spoke with Power BI Super User, Greg Deckler, about his charity work

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Top Kudoed Authors
Users Online
Currently online: 5 members 3,052 guests
Please welcome our newest community members: