Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Created April 1, 2025 21:39
Show Gist options
  • Select an option

  • Save cbaragao/4f50bcf827ac50d2ba24056fdefd7ffb to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/4f50bcf827ac50d2ba24056fdefd7ffb to your computer and use it in GitHub Desktop.
/*
Benefit: Automatically removes projections from consideration
Make sure Dates[Year] and Dates[Month Name] are on the visual.
Example for a Matrix put these columns in the Rows well in this order
- Year
- Month Name
*/
// Select a measure
var offsetMeasure = Model.SelectMeasure();
// Select an offset column
string offsetColumn = "CurrMonthOffset";
// Build DAX string
string dax =
@"
VAR __SelOffset = SELECTEDVALUE( Dates[" +
offsetColumn + "])" +
@"
VAR __Result = CALCULATE ( [" +
offsetMeasure.Name + @"], FILTER ( ALL ( Dates[" + offsetColumn +
@"], Dates[IsAfterToday]), Dates[CurrMonthOffset]<= __SelOffset && Dates[IsAfterToday] = FALSE),
REMOVEFILTERS(Dates[Month Name], Dates[Month & Year]))
RETURN
__Result
";
// Build measure name
string measureName = "YTD Offset - " + offsetMeasure.Name;
// Add the measure
Model.Tables["1M"].AddMeasure(measureName, dax, "TI\\Offsets").FormatDax();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment