1

I have some M code that returns a value from a specified row on a named table in Excel (called fParameters):

let Parameter=(TableName,RowNumber) =>
    let
        Source = Excel.CurrentWorkbook(){[Name=TableName]}[Content],
        value = Source{RowNumber-1}[Value]
    in
        value
in Parameter  

I use the function to set where I want to load the raw data from:

Source = Excel.Workbook(File.Contents(fParameters("Parameters",1) & fParameters("Parameters",4)),null, true),
S1_Sheet = Source{[Item="S1",Kind="Sheet"]}[Data],

The code above will look at rows 1 and 4 of the Parameters table to return the source file. It will then get the data from the S1 worksheet on that source file.

The Problem:
I would like to change the function so that I can change the name of the column it looks at. At the moment it looks at the [Value] column. I have tried adding ColumnName as the third parameter for the function, but can't get it to look at that column - at best it looks for a column called ColumnName.

let Parameter=(TableName,RowNumber,ColumnName) =>
    let
        Source = Excel.CurrentWorkbook(){[Name=TableName]}[Content],
        value = Source{RowNumber-1}[ColumnName]
    in
        value
in Parameter  

I've tried variations with the curly brackets, but can't get it to work. Could someone point me in the right direction please?

2 Answers 2

1

Try

(TableName,RowNumber,ColumnName) =>
let
Source = Excel.CurrentWorkbook(){[Name=TableName]}[Content],
value = Table.Column(Source,ColumnName){RowNumber-1}
in value
Sign up to request clarification or add additional context in comments.

1 Comment

This is a tough one - two answers at the same time, both work and are very similar. Going for this one as it returns the value, while the one from @Ricardo returns a table (I think - this one has the ABC icon next to it, Ricardos has a small 2-column table icon). Thanks both for the answers :)
1

One way to do it is just select the column and then refer to the row

let Parameter=(TableName,RowNumber,ColumnName) =>
    let
        Source = Excel.CurrentWorkbook(){[Name=TableName]}[Content],
        SelectColumn = Table.SelectColumns(Source, ColumnName),
        SelectValue = SelectColumn{RowNumber-1}
    in
        SelectValue
in Parameter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.