From 85c9f804b171f92d3335b7c765ffdec95aa79e36 Mon Sep 17 00:00:00 2001 From: Alberto Spelta Date: Mon, 19 Aug 2024 15:06:37 +0200 Subject: [PATCH 1/2] Extend column statistics analysis to include columns with GroupByColumns This commit fixes an issue where the cardinality of a column would result in zero if the column had `GroupByColumns` specified and `IsAvailableInMDX=FALSE`. This occurred because statistics for columns with IsAvailableInMDX set to FALSE are not available in the DMV $SYSTEM.DISCOVER_STORAGE_TABLES (see WHERE LEFT(TABLE_ID, 2) = 'H$' in PopulateColumnsCardinality) --- src/Dax.Model.Extractor/StatExtractor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Dax.Model.Extractor/StatExtractor.cs b/src/Dax.Model.Extractor/StatExtractor.cs index 095d28c..4c52b27 100644 --- a/src/Dax.Model.Extractor/StatExtractor.cs +++ b/src/Dax.Model.Extractor/StatExtractor.cs @@ -257,7 +257,7 @@ private void LoadColumnStatistics(bool analyzeDirectQuery, DirectLakeExtractionM // skip direct query tables if the analyzeDirectQuery is false where t.Columns.Count > 1 && (analyzeDirectQuery || !t.HasDirectQueryPartitions) from c in t.Columns - where c.State == "Ready" && !c.IsRowNumber && c.GroupByColumns.Count == 0 + where c.State == "Ready" && !c.IsRowNumber // only include the column if the table does not have Direct Lake partitions or if they are resident or if analyzeDirectLake is true && (!t.HasDirectLakePartitions || (analyzeDirectLake >= DirectLakeExtractionMode.ResidentOnly && c.IsResident) From 662c07a1fb3faca9255bdfbc8e23c8a224c568cd Mon Sep 17 00:00:00 2001 From: Alberto Spelta Date: Mon, 19 Aug 2024 15:07:32 +0200 Subject: [PATCH 2/2] Updated distinct count expression to always use `COUNTROWS( ALLNOBLANKROW(..) )` --- src/Dax.Model.Extractor/StatExtractor.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Dax.Model.Extractor/StatExtractor.cs b/src/Dax.Model.Extractor/StatExtractor.cs index 4c52b27..1838a74 100644 --- a/src/Dax.Model.Extractor/StatExtractor.cs +++ b/src/Dax.Model.Extractor/StatExtractor.cs @@ -237,9 +237,10 @@ private static string EscapeTableName(Table table) private static string DistinctCountExpression(Column column) { - return column.GroupByColumns.Count == 0 - ? $"DISTINCTCOUNT({EscapeColumnName(column)})" - : $"COUNTROWS(ALLNOBLANKROW({EscapeColumnName(column)}))"; + // We always use COUNTROWS(ALLNOBLANKROW(t[c])) instead of DISTINCTCOUNT(t[c]) because it is compatible with GroupByColumns settings, such as Fields Parameters. + // COUNTROWS(ALLNOBLANKROW()) always reads the list of values from the attribute hierarchy (when AvailableInMDX=true) or queries the table if the hierarchy is not available (when AvailableInMDX=false) + + return $"COUNTROWS(ALLNOBLANKROW({EscapeColumnName(column)}))"; } private static string EscapeColumnName(Column column)