Replies: 14 comments 15 replies
-
While I agree this should be a built-in feature, the short term solution is the following script (tested and works in both TE2 and TE3!): // Before running the script, select the measures or columns that you
// would like to use as field parameters (hold down CTRL to select multiple
// objects). Also, you may change the name of the field parameter table
// below. NOTE: If used against Power BI Desktop, you must enable unsupported
// features under File > Preferences (TE2) or Tools > Preferences (TE3).
var name = "Parameter";
if(Selected.Columns.Count == 0 && Selected.Measures.Count == 0) throw new Exception("No columns or measures selected!");
// Construct the DAX for the calculated table based on the current selection:
var objects = Selected.Columns.Any() ? Selected.Columns.Cast<ITabularTableObject>() : Selected.Measures;
var dax = "{\n " + string.Join(",\n ", objects.Select((c,i) => string.Format("(\"{0}\", NAMEOF('{1}'[{0}]), {2})", c.Name, c.Table.Name, i))) + "\n}";
// Add the calculated table to the model:
var table = Model.AddCalculatedTable(name, dax);
// In TE2 columns are not created automatically from a DAX expression, so
// we will have to add them manually:
var te2 = table.Columns.Count == 0;
var nameColumn = te2 ? table.AddCalculatedTableColumn(name, "[Value1]") : table.Columns["Value1"] as CalculatedTableColumn;
var fieldColumn = te2 ? table.AddCalculatedTableColumn(name + " Fields", "[Value2]") : table.Columns["Value2"] as CalculatedTableColumn;
var orderColumn = te2 ? table.AddCalculatedTableColumn(name + " Order", "[Value3]") : table.Columns["Value3"] as CalculatedTableColumn;
if(!te2) {
// Rename the columns that were added automatically in TE3:
nameColumn.IsNameInferred = false;
nameColumn.Name = name;
fieldColumn.IsNameInferred = false;
fieldColumn.Name = name + " Fields";
orderColumn.IsNameInferred = false;
orderColumn.Name = name + " Order";
}
// Set remaining properties for field parameters to work
// See: https://twitter.com/markbdi/status/1526558841172893696
nameColumn.SortByColumn = orderColumn;
nameColumn.GroupByColumns.Add(fieldColumn);
fieldColumn.SortByColumn = orderColumn;
fieldColumn.SetExtendedProperty("ParameterMetadata", "{\"version\":3,\"kind\":2}", ExtendedPropertyType.Json);
fieldColumn.IsHidden = true;
orderColumn.IsHidden = true; |
Beta Was this translation helpful? Give feedback.
-
Thanks for providing this... what about updating an existing field parameter? What would be the code to drop/add a field parameter table or is there a way to "update" and existing FP? |
Beta Was this translation helpful? Give feedback.
-
There is a System.NullReferenceException error on line 36 (GroupByColumn) when model doesn't have the groupbycolumn field |
Beta Was this translation helpful? Give feedback.
-
I get the same error. Is there a solution for this issue? |
Beta Was this translation helpful? Give feedback.
-
@sshoj and @TheoJoore what is the compatibility level and mode of your models? The |
Beta Was this translation helpful? Give feedback.
-
@otykier , thnx for you reply. I want to implement this in an Azure Analysis Services model with compatability level 1500. If I set it local to for example 1510 the error in Tabular Editor is gone, but then when I deploy it to the Azure Analysis Server I get this message |
Beta Was this translation helpful? Give feedback.
-
As I said above, this only works for Power BI models - not SSAS / AzureAS models. Microsoft documentation for GroupByColumns:
|
Beta Was this translation helpful? Give feedback.
-
So to be clear: there is no option to add a field parameter to an AzureAS model? |
Beta Was this translation helpful? Give feedback.
-
Correct. You can, however, use the DirectQuery over AS feature in Power BI, to build a Power BI model based on an AzureAS database, to which you can then add a field parameter: https://learn.microsoft.com/en-us/power-bi/create-reports/power-bi-field-parameters#limitations
|
Beta Was this translation helpful? Give feedback.
-
This code generated a correct looking calculated table that wouldnt compile for its life. In the end the markup was the problem. After removing any spaces or tabs generated by the code and making the markup myself the calculated table worked instantly. Would still like this as a feature in TE3! |
Beta Was this translation helpful? Give feedback.
-
@otykier - this would be great if in TE2 you could select columns from multiple tables when holding CTRL, I can select many columns from one table, or many tables, but not columns from different tables. What am I missing?? |
Beta Was this translation helpful? Give feedback.
-
Is the #C still the way to do this or have it been added as functionality to the TE. User would like to be able to add it as local measure in the data model, level 1570. |
Beta Was this translation helpful? Give feedback.
-
Hello @otykier , Good day !! I'm using Tabular Editor 2.21.1 version to create field parameters for my direct lake semantic model. I'm following the C# code to create field parameters which you have provided. While running the C# script, I'm unable to select columns from multiple tables even though the tables have relationship with each other. Please let me know if I need to enable any settings in Tabular editor or the creation of field parameters is not available in free versions of Tabular editor. Looking forward for your response. Thanks |
Beta Was this translation helpful? Give feedback.
-
But none of this works with SSAS models. Unclear why. |
Beta Was this translation helpful? Give feedback.
-
This is a new May 2022 desktop feature, and it apparently goes beyond the DAX with a number properties that need to be set in order for Tabular to recognize it.
A full thread of the details can be found at https://twitter.com/markbdi/status/1526602566486081536?s=20&t=1Z2Ny-GTJlSeKocTxneX3g
Would be nice if we could right-click on Table in the TOM, and create a new Field Parameter table, with a bit of assist on the DAX (one or two rows of example code would be sufficient I think, no need initially for a full UI like PBID has) with all underlying properties set correctly.
Beta Was this translation helpful? Give feedback.
All reactions