Skip to content

Commit

Permalink
Merge pull request #37 from sql-bi/alberto/issue-9
Browse files Browse the repository at this point in the history
Fix "Item '%' already exists in the collection"
  • Loading branch information
marcosqlbi authored Jan 9, 2023
2 parents 6826feb + 7719ac4 commit 3ce19f9
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/Dax.Template/Measures/MeasureTemplateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ public override string? Expression
public const string ENTITY_COLUMNS_TABLE = "CT";
internal static TabularMeasure? FindMeasure(TabularModel model, string measureName)
{
return
(from t in model.Tables
from m in t.Measures
select m).FirstOrDefault(m => m.Name == measureName);
foreach (var table in model.Tables)
{
var measure = table.Measures.Find(measureName);
if (measure != null)
return measure;
}

return null;
}

string GetDefaultVariable(string expression)
Expand Down Expand Up @@ -154,16 +158,9 @@ public virtual TabularMeasure ApplyTemplate(TabularModel model, Table targetTabl
var clonedMeasure = measure.Clone();
(measure.Parent as Table)?.Measures.Remove(measure);
measure = clonedMeasure;
try
{
targetTable.Measures.Add(measure);
}
catch (Exception ex)
{
// TODO: remove try/catch after the issue has been closes https://github.com/sql-bi/DaxTemplate/issues/9
throw new TemplateUnexpectedException($" *** PLEASE REPORT THIS ISSUE ON GITHUB *** { ex.Message }", ex);
}
targetTable.Measures.Add(measure);
}
measure.Name = Name; // Force rename in case of different char casing (e.g. 'Amount' renamed to 'amount')
measure.FormatString = FormatString ?? ReferenceMeasure?.FormatString;
measure.IsHidden = IsHidden;
measure.DisplayFolder = DisplayFolder;
Expand Down

0 comments on commit 3ce19f9

Please sign in to comment.