Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hi,
I am using Microsoft.AnalysisServices.Tabular to alter the dataset via C# code.
i have a table called Table A and there is another Table Called Table A Custom, Now when i try to add a column in Table A Custom it also appears on Table A some how, I have debugged the whole case and have seen that through code it is only updating a single table Table A Custom.
// tables contain ['Table A Custom','Table B Custom']
for( tableName in tables){
var pbiTable = database.Model.Tables
.FirstOrDefault(x => x.Name.Equals(tableName, StringComparison.OrdinalIgnoreCase));
var pbiColumns = pbiTable?.Columns;
var fieldsToAdd = getFieldsFromDb();
fieldsToAdd.ForEach(field => AddField(field.Name, field.DataType, pbiColumns));
}
// Function to add the column
private void AddField(string fieldName, DataType fieldType, ColumnCollection pbiColumns)
{
if (pbiColumns == null)
{
return;
}
var newColumn = new Microsoft.AnalysisServices.Tabular.DataColumn
{
Name = fieldName,
DataType = fieldType,
SourceColumn = fieldName,
LineageTag = fieldName + "-" + Guid.NewGuid(),
};
pbiColumns.Add(newColumn);
}Note: This behaviour is intermittent, does not happen everytime i try to do, but it usually happens when i freshly publish the dataset to service and try to add fields to the table via code. also note sometimes the fields do not appear quickly but appears after a refresh to that dataset. i am unable to find the root cause for the same
Hi @ChetanK004
Thank you for reaching out to the Microsoft Fabric Forum Community.
@Zanqueta Thanks for the inputs.
I hope the information provided by user was helpful. If you still have questions, please don't hesitate to reach out to the community.
Hi @Zanqueta , Thanks for the response, For the below root causes
var pbiTable = database.Model.Tables
.FirstOrDefault(x => x.Name.Equals(tableName, StringComparison.OrdinalIgnoreCase));
if (pbiTable == null)
throw new Exception($"Table {tableName} not found.");Add columns safely
private void AddField(string fieldName, DataType fieldType, ColumnCollection pbiColumns)
{
if (pbiColumns == null) return;
var newColumn = new Microsoft.AnalysisServices.Tabular.DataColumn
{
Name = fieldName,
DataType = fieldType,
SourceColumn = fieldName
};
pbiColumns.Add(newColumn);Use the recomended refresh method and order For schema changes:
Model.RequestRefresh(RefreshType.Full);
database.Model.SaveChanges();
References:
Model.RequestRefresh Method (Microsoft.AnalysisServices.Tabular) | Microsoft Learn
Please, let me know if it worked.
Hi @ChetanK004,
This behaviour is related to how Microsoft.AnalysisServices.Tabular manages object references in the Tabular Object Model (TOM). When you add a column to a table, the change is applied to the in-memory model, and then persisted when you call Model.SaveChanges(). If columns appear in another table, it usually indicates one of the following:
Please, look at steps bellow an try:
var pbiTable = database.Model.Tables
.FirstOrDefault(x => x.Name.Equals(tableName, StringComparison.OrdinalIgnoreCase));
if (pbiTable == null)
throw new Exception($"Table {tableName} not found.");
var pbiColumns = pbiTable.Columns;database.Model.SaveChanges();The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 4 | |
| 3 | |
| 2 | |
| 1 | |
| 1 |
| User | Count |
|---|---|
| 12 | |
| 7 | |
| 4 | |
| 3 | |
| 3 |