示例#1
0
        static void Main(string[] args)
        {
            string connectString = args.Length >= 1 ? args[0] : "localhost:53610";

            Server server = new Server();

            server.Connect(connectString);

            Model model = server.Databases[0].Model;

            DaxFormatter.FormatDaxForModel(model);
        }
        public static void FormatDaxForModel(Model model)
        {
            Console.WriteLine("Iterating measure, calculated columns and calulated tables for DAX formatting...");
            Console.WriteLine();

            foreach (Table table in model.Tables)
            {
                // check which measures require formatting
                foreach (var measure in table.Measures)
                {
                    Console.Write("Measure: " + table.Name + "[" + measure.Name + "]");
                    if (MeasureRequiresFormatting(measure))
                    {
                        Console.WriteLine(" - formatting DAX...");
                        string expressionOwner        = measure.Name;
                        string originalDaxExpression  = measure.Expression;
                        string formattedDaxExpression = DaxFormatter.FormatDaxExpression(originalDaxExpression, expressionOwner);
                        // write formatted expression back to measure
                        measure.Expression = formattedDaxExpression;
                        // store hash of formatted expression for later comparison
                        string hashedDaxExpression = GetHashValueAsString(formattedDaxExpression);
                        if (measure.Annotations.Contains("HashedExpression"))
                        {
                            measure.Annotations["HashedExpression"].Value = hashedDaxExpression;
                        }
                        else
                        {
                            measure.Annotations.Add(new Annotation {
                                Name = "HashedExpression", Value = hashedDaxExpression
                            });
                        }
                    }
                    else
                    {
                        Console.WriteLine(" - DAX already formatted");
                    }
                }

                // check which calculated columns require formatting
                foreach (var column in table.Columns)
                {
                    if (column.Type == ColumnType.Calculated)
                    {
                        CalculatedColumn col = (CalculatedColumn)column;
                        Console.Write("Calculated column: " + table.Name + "[" + col.Name + "]");
                        if (CalculatedColumnRequiresFormatting(col))
                        {
                            Console.WriteLine(" - formatting DAX...");
                            string expressionOwner        = "'" + table.Name + "'[" + col.Name + "]";
                            string originalDaxExpression  = col.Expression;
                            string formattedDaxExpression = DaxFormatter.FormatDaxExpression(originalDaxExpression, expressionOwner);
                            // write formatted expression back to calculated column
                            col.Expression = formattedDaxExpression;
                            // store hash of formatted expression for later comparison
                            string hashedDaxExpression = GetHashValueAsString(formattedDaxExpression);
                            if (col.Annotations.Contains("HashedExpression"))
                            {
                                col.Annotations["HashedExpression"].Value = hashedDaxExpression;
                            }
                            else
                            {
                                col.Annotations.Add(new Annotation {
                                    Name = "HashedExpression", Value = hashedDaxExpression
                                });
                            }
                        }
                        else
                        {
                            Console.WriteLine(" - DAX already formatted");
                        }
                    }
                }

                // check which calculated tables require formatting
                if ((table.Partitions.Count > 0) &&
                    (table.Partitions[0].SourceType == PartitionSourceType.Calculated))
                {
                    Console.Write("Calculated table: " + table.Name);
                    if (CalculatedTableRequiresFormatting(table))
                    {
                        var source = table.Partitions[0].Source as CalculatedPartitionSource;
                        Console.WriteLine(" - formatting DAX...");
                        string expressionOwner        = table.Name;
                        string originalDaxExpression  = source.Expression;
                        string formattedDaxExpression = DaxFormatter.FormatDaxExpression(originalDaxExpression, expressionOwner);
                        // write formatted expression back to calculated column
                        source.Expression = formattedDaxExpression;
                        // store hash of formatted expression for later comparison
                        string hashedDaxExpression = GetHashValueAsString(formattedDaxExpression);
                        if (table.Annotations.Contains("HashedExpression"))
                        {
                            table.Annotations["HashedExpression"].Value = hashedDaxExpression;
                        }
                        else
                        {
                            table.Annotations.Add(new Annotation {
                                Name = "HashedExpression", Value = hashedDaxExpression
                            });
                        }
                    }
                    else
                    {
                        Console.WriteLine(" - DAX already formatted ");
                    }
                }
            }

            model.RequestRefresh(RefreshType.Automatic);
            model.SaveChanges();

            Console.WriteLine();
            Console.WriteLine("Press any key to continue");
            Console.ReadLine();
        }