OpenBI Docs Buy

SUBSTITUTEWITHINDEX

A table's rows with the columns it shares with an index table replaced by the matching row's position in that table, in an order.

Group: Table manipulation. Compatible with DAX.

Syntax

SUBSTITUTEWITHINDEX ( <table>, <indexColumnName>, <indexColumnsTable>,
    <orderBy_expression> [, <order>] [, <orderBy_expression> [, <order>]] … )
ParameterTypeDescription
tableTableThe rows to rewrite.
indexColumnNameTextWhat to call the column of positions, as a text.
indexColumnsTableTableThe table that gives each combination its position. Its columns in common with the first table are the ones replaced.
orderBy_expressionExpression
repeatable, evaluated for each row
A value worked out for each row of the index table that sets its order.
orderKeyword
optional, repeatable
ASC or DESC (when left out), for the order value before it; 1 or TRUE, 0 or FALSE say the same.

Returns

Table. Each row of the table that finds a match in the index table, with the shared columns swapped for one whole-number column: the match's position, counting from 0.

Remarks

  • The shared columns are found as in NATURALINNERJOIN: the same name for tables made in the query, the same column of the model otherwise.
  • A row without a match in the index table is left out.
  • The positions follow the index table in the order given. Without an order keyword the order is DESC, the largest first; write ASC or DESC after every order value to make the numbering plain to read.
  • Power BI's own queries use it to number the columns of a matrix; in a report it is rarely needed.

Examples

Products replaced by their position

Sorted by name from A, Cider is 0, IPA 1 and Pils 2.

EVALUATE
VAR Sold =
    DATATABLE (
        "Shop", STRING, "Product", STRING, "Liters", INTEGER,
        { { "A", "Pils", 120 }, { "A", "IPA", 80 }, { "B", "Pils", 30 }, { "B", "Cider", 60 } }
    )
VAR Beers = DATATABLE ( "Product", STRING, { { "Pils" }, { "IPA" }, { "Cider" } } )
RETURN
    SUBSTITUTEWITHINDEX ( Sold, "Product index", Beers, [Product], ASC )
ORDER BY [Shop], [Product index]
ShopLitersProduct index
A801
A1202
B600
B302

A row without a match

Stout is not in the index table, so its row is left out.

EVALUATE
VAR Sold =
    DATATABLE (
        "Shop", STRING, "Product", STRING, "Liters", INTEGER,
        { { "A", "Pils", 120 }, { "A", "IPA", 80 }, { "B", "Pils", 30 }, { "B", "Stout", 60 } }
    )
VAR Beers = DATATABLE ( "Product", STRING, { { "Pils" }, { "IPA" }, { "Cider" } } )
RETURN
    SUBSTITUTEWITHINDEX ( Sold, "Product index", Beers, [Product], DESC )
ORDER BY [Shop], [Product index]
ShopLitersProduct index
A1200
A801
B300

Two columns replaced by one

EVALUATE
VAR Sold =
    DATATABLE (
        "Shop", STRING, "Product", STRING, "Liters", INTEGER,
        { { "A", "Pils", 120 }, { "A", "IPA", 80 }, { "B", "Pils", 30 } }
    )
VAR Keys =
    DATATABLE (
        "Shop", STRING, "Product", STRING,
        { { "A", "Pils" }, { "A", "IPA" }, { "B", "Pils" } }
    )
RETURN
    SUBSTITUTEWITHINDEX ( Sold, "Key", Keys, [Shop], ASC, [Product], DESC )
ORDER BY [Key]
LitersKey
1200
801
302

NATURALINNERJOIN, RANKX, TOPN, SELECTCOLUMNS.

Last updated · OpenBI Studio 0.3.74