Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add ability to Add a Field Parameter table #541

Closed
edhans opened this issue May 17, 2022 · 11 comments
Closed

Feature Request: Add ability to Add a Field Parameter table #541

edhans opened this issue May 17, 2022 · 11 comments
Assignees
Labels
feature: enhancement Request is a enchacement to existing feature

Comments

@edhans
Copy link

edhans commented May 17, 2022

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.

@otykier
Copy link
Collaborator

otykier commented May 17, 2022

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;

@otykier otykier added the feature: enhancement Request is a enchacement to existing feature label May 17, 2022
@dpesante
Copy link

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?

@sshoj
Copy link

sshoj commented Oct 24, 2022

There is a System.NullReferenceException error on line 36 (GroupByColumn) when model doesn't have the groupbycolumn field
Is these anyway to avoid that?

@TheoJoore
Copy link

I get the same error. Is there a solution for this issue?

@otykier
Copy link
Collaborator

otykier commented Nov 4, 2022

@sshoj and @TheoJoore what is the compatibility level and mode of your models? The GroupByColumns property is only available for Power BI models at CL 1400 or above.

@TheoJoore
Copy link

@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
image

@otykier
Copy link
Collaborator

otykier commented Nov 4, 2022

As I said above, this only works for Power BI models - not SSAS / AzureAS models.

Microsoft documentation for GroupByColumns:

This metadata object is only supported at database compatibility level of 1400 or above for Pbi server.

@TheoJoore
Copy link

So to be clear: there is no option to add a field parameter to an AzureAS model?

@otykier
Copy link
Collaborator

otykier commented Nov 4, 2022

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

Currently you can't create parameters in live connection data sources, without a local model. However, using DirectQuery for Power BI datasets and AS (preview), you can add a local model to create field parameters. Read more about DirectQuery for Power BI datasets and AS (preview).

@luukhendrikscordstrap
Copy link


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;

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!

@mark-endicott
Copy link

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;

@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??

@TabularEditor TabularEditor locked and limited conversation to collaborators Oct 12, 2023
@mlonsk mlonsk converted this issue into discussion #1100 Oct 12, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
feature: enhancement Request is a enchacement to existing feature
Development

No branches or pull requests

8 participants