GENERATESERIES, DATATABLE and ROW: build tables in DAX

How to build small tables in DAX, GENERATESERIES for number ranges and what-if parameters, DATATABLE for typed inline tables, ROW and the table constructor for quick rows, SELECTCOLUMNS to rename, and examples for bins, scenarios and helper tables.

Short answer: DAX can build small tables without a data source. GENERATESERIES ( start, end, step ) returns a one-column table named Value with an arithmetic series (0 to 0.3 in steps of 0.05), the basis of what-if parameters and bins. DATATABLE declares a typed table with constant values. The table constructor { ( "A", 1 ), ( "B", 2 ) } builds rows from any expressions, and ROW ( "Name", expr, ... ) builds one row with named columns. Use them for calculated tables, slicer tables and inside measures.

Every disconnected slicer (scenario, discount, top N, bin size) needs a small table. Typing it in DAX keeps it in the model's code, versioned and documented.

What does each one build?

FunctionBuildsColumn names
GENERATESERIES ( 0, 0.3, 0.05 )0, 0.05, ... 0.3Value
{ 1, 2, 3 }one columnValue
{ ( "Low", 0 ), ( "High", 1 ) }several columnsValue1, Value2
DATATABLE ( "Name", STRING, "Order", INTEGER, { { "Low", 1 }, { "High", 2 } } )typed columnsas declared
ROW ( "Revenue", [Revenue], "Margin", [Margin] )one rowas declared
Table builders

How do you build a what-if parameter by hand?

-- calculated table
Discount = GENERATESERIES ( 0, 0.3, 0.05 )

-- measures
Discount Value = SELECTEDVALUE ( Discount[Value], 0 )
Revenue after Discount = [Revenue] * ( 1 - [Discount Value] )

A slicer on Discount[Value], formatted as a percentage, lets readers try 0% to 30%. Modeling, New parameter, Numeric range builds the same table and measure for you. The sequence stops at the last value at or below end, and the increment must be positive.

How do you define a typed lookup table?

Scenario =
DATATABLE (
    "Scenario", STRING,
    "Growth", DOUBLE,
    "Order", INTEGER,
    {
        { "Pessimistic", -0.05, 1 },
        { "Base", 0.03, 2 },
        { "Optimistic", 0.08, 3 }
    }
)

Forecast =
[Revenue LY] * ( 1 + SELECTEDVALUE ( Scenario[Growth], 0.03 ) )

Sort Scenario[Scenario] by Scenario[Order]. DATATABLE accepts only constants; types are INTEGER, DOUBLE, STRING, BOOLEAN, CURRENCY and DATETIME.

When do you use the table constructor and ROW?

The table constructor accepts any scalar expressions, which DATATABLE does not:

-- bins for a histogram, with labels
Bins =
SELECTCOLUMNS (
    { ( "0 to 99", 0, 100 ), ( "100 to 499", 100, 500 ), ( "500+", 500, 1E9 ) },
    "Bin", [Value1], "From", [Value2], "To", [Value3]
)

-- a one-row table of KPIs in a DAX query
EVALUATE
ROW ( "Revenue", [Revenue], "Margin %", [Margin %], "Customers", [Customers] )

SELECTCOLUMNS renames the generic Value1, Value2 columns. Use the bins table with a measure that counts customers whose revenue falls between From and To (see the histogram post).

How else are generated tables useful in measures?

-- months 1 to 12 as a series, to fill gaps
Months = GENERATESERIES ( 1, 12, 1 )

-- check whether the selection is in a fixed list
Is Core Market = SELECTEDVALUE ( Customer[Country] ) IN { "DE", "AT", "CH" }

What goes wrong?

  • Value column name. GENERATESERIES and the one-column constructor name the column Value; rename with SELECTCOLUMNS if two such tables meet.
  • Floating-point steps. GENERATESERIES ( 0, 1, 0.1 ) can produce 0.30000000000000004; round with ROUND ( [Value], 2 ) in an added column.
  • DATATABLE with expressions. Only constants are allowed; use the constructor.
  • Slicer tables connected by mistake. Keep what-if tables disconnected unless the relationship is meant.

Where can you check this?

Where do you start?

Add one scenario table with DATATABLE and a forecast measure that reads it. Table functions lists every function that returns a table.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.