Excel catalyst 50th wave -Excel open source and PowerBIDeskTop interconnection of Title IV

Promised overall share, also will promise in the end, to share this post PowerBI function last post, about how to export metadata data model, data dictionary definition of the author as a model.

Cipian corresponding functions implemented by: Wave 6 - Exporting PowerbiDesktop model data dictionary https://www.jianshu.com/p/bc26a8dcdfce

Relational database data dictionary, there must be more analytical database data dictionary, DAX modeling process, if the relational model complex, too many references to the table, the resulting measure, calculated columns too, just to view the model from PowerBIDeskTop relationship is very inefficient.

Of course the best way to see, in the whole universe, only to find Excel, and no other would dare to challenge it, it will naturally PowerBIDeskTop metadata model, stored in the Excel cell Gerry, combined with the sort of Excel, screening, condition format, like cell formatting friendly auxiliary processing operation Now model metadata.

First on the code, the same way the essence of this code also includes the development of a number of VSTO Excel - Use ListObject object to return data.

        public static void OutputDataDic(string cnnString, string dbName, string fileName)
        {

            string shtName = "数据字典_" + fileName;
            shtName = shtName.Length > 31 ? shtName.Substring(0, 31) : shtName;
            Excel.Worksheet wht;
            try
            {
                wht = Common.ExcelApp.ActiveWorkbook.Worksheets[shtName];
                Common.ExcelApp.DisplayAlerts = false;
                wht.Delete();
                Common.ExcelApp.DisplayAlerts = true;
            }
            catch (Exception)
            {

            }
            wht = Common.ExcelApp.Worksheets.Add();
            wht.Name = shtName;
            var vstoSht = Globals.Factory.GetVstoObject(wht);

            ListObject listMeasures = vstoSht.Controls.AddListObject(vstoSht.Range["A1"], "度量值表_" + fileName);
            ListObject listDims = vstoSht.Controls.AddListObject(vstoSht.Range["C1"], "维度表_" + fileName);
            ListObject listRelationShips = vstoSht.Controls.AddListObject(vstoSht.Range["E1"], "关系表_" + fileName);

            using (AMO.Server server = new AMO.Server())
            {
                server.Connect(cnnString);
                AMO.Database pbidDb = server.Databases[dbName];

                ////获取度量值
                GetMeasuresTable(pbidDb, listMeasures);
                //获取列字段
                GetDimsTable(pbidDb, listDims);
                ////获取关系
                GetRelationsTable(pbidDb, listRelationShips);

            };
            vstoSht.Activate();
        }

Get metadata, there are measures, dimensions fields, and relationships between tables, three types of metadata. The core code is as follows (in the above method, starting with the visit to the SSAS metadata Sqlserver have to distinguish between different versions compatible models 1200 and 1100 wrote two sets of code, due to the presence of more than 1200 and compatible models PowerBIDeskTop only, so 1100 the version has been cleared):

        private static DataSet.DataSet1.MeasuresOfLevel1200DataTable GetMeasuresTableOfLevel1200(AMO.Database tabularDb)
        {
            TOM.Model model = tabularDb.Model;
            DataSet.DataSet1.MeasuresOfLevel1200DataTable measuresTable = new DataSet.DataSet1.MeasuresOfLevel1200DataTable();
            foreach (TOM.Table table in model.Tables)
            {
                foreach (TOM.Measure measure in table.Measures)
                {
                    DataSet.DataSet1.MeasuresOfLevel1200Row dr = measuresTable.NewMeasuresOfLevel1200Row();
                    dr.Measure = $"[{measure.Name}]";
                    dr.TableName = table.Name;
                    dr.DisplayFolder = measure.DisplayFolder;
                    dr.Description = measure.Description;
                    dr.FormatString = measure.FormatString;
                    dr.MeasureExpression = measure.Expression;
                    dr.Visible = !measure.IsHidden;
                    dr.DataType = measure.DataType.ToString();
                    measuresTable.AddMeasuresOfLevel1200Row(dr);
                }
            }

            return measuresTable;
        }
 private static DataTable GetDimsTableOfLevel1200(AMO.Database tabularDb)
        {
            TOM.Model model = tabularDb.Model;
            DataSet.DataSet1.ColumnNamesDataTable dtColumns = new DataSet.DataSet1.ColumnNamesDataTable();
            foreach (TOM.Table table in model.Tables)
            {
                foreach (TOM.Column col in table.Columns)
                {
                    if (!col.Name.Contains("RowNumber"))
                    {
                        TOM.CalculatedColumn calCol = col as TOM.CalculatedColumn;
                        DataSet.DataSet1.ColumnNamesRow dr = dtColumns.NewColumnNamesRow();
                        dr.TableName = table.Name;
                        dr.FieldName = col.Name;
                        dr.FieldType = col.DataType.ToString();
                        if (calCol != null)
                        {
                            dr.DaxExpression = calCol.Expression;
                        }

                        dr.Description = col.Description;
                        dr.Visible = !col.IsHidden;
                        dtColumns.AddColumnNamesRow(dr);
                    }

                }
            }
            return dtColumns.Cast<DataSet.DataSet1.ColumnNamesRow>().Where(s => !s.TableName.StartsWith("LocalDateTable"))
                                                            .Where(s => !s.TableName.StartsWith("DateTableTemplate"))
                                                            .CopyToDataTable<DataSet.DataSet1.ColumnNamesRow>();
        }
        private static DataTable GetRelationsTableOfLevel1200(AMO.Database tabularDb)
        {
            TOM.Model model = tabularDb.Model;
            DataSet.DataSet1.关系表DataTable dt = new DataSet.DataSet1.关系表DataTable();
            foreach (TOM.Relationship relationship in model.Relationships)
            {
                TOM.SingleColumnRelationship singleRelationShip = relationship as TOM.SingleColumnRelationship;
                DataSet.DataSet1.关系表Row dr = dt.New关系表Row();
                dr.ToTable = relationship.ToTable.Name;
                dr.FromTable = relationship.FromTable.Name;
                dr.IsActive = relationship.IsActive;
                if (singleRelationShip != null)
                {
                    dr.FromColumn = singleRelationShip.FromColumn.Name;
                    dr.ToColumn = singleRelationShip.ToColumn.Name;
                    dr.FromCardinality = singleRelationShip.FromCardinality.ToString();
                    dr.ToCardinality = singleRelationShip.ToCardinality.ToString();
                }
                dt.Add关系表Row(dr);
            }
            return dt.Cast<DataSet.DataSet1.关系表Row>().Where(s => !s.ToTable.StartsWith("LocalDateTable")).CopyToDataTable<DataSet.DataSet1.关系表Row>();
        }

Epilogue

Four continuous high-quality open source article with everyone to go into the deep end Excel catalyst, senior, high commercial value of the core code to run the world's leading full-Excel and PowerBIDeskTop of interconnection.

Finally, I wish PowerBI Chinese community is getting better, prosper again, more willing to share a latecomer to redouble their efforts.

Technical exchange QQ group

QQ group name: Excel catalyst open discussion groups, QQ group number: 788 145 319
Excel catalyst dimensional code open source discussion groups

About Excel catalyst

Excel catalyst name, first a public micro-channel number, then shun its name, officially launched the Excel plug-in, plug-continuous updates, the update cycle depends on my time may be able to fight for one week on a line function modules. Excel catalyst plug-permanent commitment to individual users free of charge!

Excel catalyst plugin uses the latest deployment of technology, once installed, all future updates automatically update is complete, no need to repeat concerns updates, manually download the installation package to reinstall, you can always keep the latest version with a single installation!

The catalyst Excel plug-in download link: https://pan.baidu.com/s/1Iz2_NZJ8v7C9eqhNjdnP3Q

Contact the author

No public

Named catalyst , due to strong itself, not everyone can immediately enjoy Excel, most people are still at the stage of the child's Excel software, the mind is very clear and want to achieve results, and experts who also have achieved it, that is, how to get yourself out Dounong, or worse, you do not know what Excel can do while staying in constant repetition, mechanically, manually doing data, spend countless youth years. So spawned whether as a medium, so that the majority of Excel users can instantly ignite the explosion point Excel, without the need desperately struggled day and night to learn skills, brain burn senior complex functions, going on from entry to give up the road.

Finally, Excel is powerful, in fact, the need to establish a point of view, not all things have to Excel to complete, and not all things Excel are very competent, the outside world is still a vast world, just a dazzling Excel star, there are other more exciting same powerful technology tools. * Excel catalyst will be leveraging these other technologies, so that Excel can play a more powerful eruption!

About Excel catalyst Author

Name: Li Weijian, for many years engaged in data (BI direction) analysis, a learner in the same way.
Served industries: retail category in particular shoes retail industry, electricity providers (Taobao, Lynx, Jingdong, the only product)

Technical route from an ordinary user, through the Excel software to learn, go from the data world, Coban non-IT professionals.
After many difficulties, and finally reach the plains of technical data on the road, learning many knowledge is no longer too difficult, but also formed its own set of data solutions (data collection, data cleaning and processing, multi-dimensional data modeling, data reporting display, etc.).

Specializes in technology areas: Excel and other Office family of software, the secondary development of VBA & VSTO, Sqlserver database technology, business intelligence BI technology Sqlserver of, Powerbi technology, cloud server deployment technologies.

2018 began his career made a major adjustment, from the original full-time job, turned freelance, no fixed income, temporarily on the road ahead is not clear, bitter return to full-time job, for Excel catalyst operations and development must be greatly affected (within the time could not maintain full-time job can not just put the results in a work published in time, the time outside of work is very limited, because he has thirty years of age, family responsibility).

And the majority of advocates with expectations: Excel catalyst has been able to run down, I have the benefit of the group were able to support ( multi-message encouragement, friend circle under forwarded the recommendation under small a reward and the most focused and where the company can and recommended recommended peers, so I can maximize the value of technology in your company to achieve a win-win (you can imagine how the data is preliminary consultant or small projects to develop forms of cooperation).

Guess you like

Origin www.cnblogs.com/ExcelCuiHuaJi/p/11225126.html