### Install pbixray Source: https://github.com/hugoberry/pbixray/blob/main/pbixray.ipynb Use pip to install the pbixray package in your environment. ```python !pip install pbixray ``` -------------------------------- ### Install PBIXRay Source: https://github.com/hugoberry/pbixray/blob/main/README.md Install the PBIXRay library using pip. This is the first step before using the library. ```bash pip install pbixray ``` -------------------------------- ### Python: Date and Currency Column Examples Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/vertipaq-decoder-api.md Provides example usage for handling Date and Currency columns. Raw day-count integers are converted to datetime objects, and raw cent values are converted to Decimal objects. ```python # Date column: raw values are days since 1899-12-30 raw = pd.Series([44197, 44198, 44199]) # Converts to: 2021-01-01, 2021-01-02, 2021-01-03 # Currency column: raw values are cents raw = pd.Series([10000, 25000, 15000]) # Converts to: Decimal('1'), Decimal('2.5'), Decimal('1.5') ``` -------------------------------- ### Container Usage Example Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/types.md Illustrates how to use the Container enum to conditionally load metadata based on the source file type (PBIX or XLSX). This example shows checking the container type of a loaded DataModel. ```python from pbixray.abf.data_model import Container from pbixray.loader import DataModelLoader loader = DataModelLoader('report.pbix') dm = loader.data_model if dm.container == Container.PBIX: print("Loading from PBIX - using SQLite metadata") elif dm.container == Container.XLSX: print("Loading from XLSX - using XML metadata") ``` -------------------------------- ### Minimal PBIXRay Example Source: https://github.com/hugoberry/pbixray/blob/main/AGENTS.md Load a PBIX or XLSX file and print basic model information like table names, schema, and the first few rows of a table. The file type is auto-detected. ```python from pbixray import PBIXRay model = PBIXRay("data/Adventure Works DW 2020.pbix") # or any .xlsx with Power Pivot print(model.tables) # list of table names print(model.schema.head()) # column metadata print(model.get_table(model.tables[0]).head()) ``` -------------------------------- ### Initialize PBIXRay and Get Table Data Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/architecture.md Instantiate the PBIXRay class with a PBIX file path and retrieve data for a specific table. This is the primary entry point for users. ```python from pbixray import PBIXRay model = PBIXRay('report.pbix') df = model.get_table('Sales') ``` -------------------------------- ### Get M Parameters Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves all M Parameters from the PBIX file. Returns a DataFrame with parameter definitions. ```python model = PBIXRay('report.pbix') print(model.m_parameters) ``` -------------------------------- ### Python: Currency Scaling Example Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/vertipaq-decoder-api.md Demonstrates how to scale currency values by dividing by 10,000 and converting to Decimal objects for precision. Handles null values by returning None. ```python column_data.apply(lambda x: Decimal(x) / 10000 if pd.notnull(x) else None) ``` -------------------------------- ### Checking Table Availability Before Accessing Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/errors.md This example shows how to safely access tables from a PBIX file by first listing all available tables and then checking for the existence of a specific table before attempting to retrieve it. This prevents errors if a table is missing. ```python from pbixray import PBIXRay model = PBIXRay('report.pbix') # List available tables print("Available tables:", list(model.tables)) # Safe access if 'Sales' in model.tables: sales = model.get_table('Sales') else: print("Sales table not found") ``` -------------------------------- ### Get TMSCHEMA_FORMAT_STRING_DEFINITIONS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_FORMAT_STRING_DEFINITIONS DMV. ```python @property def tmschema_format_string_definitions() -> pd.DataFrame ``` -------------------------------- ### Metadata Usage Example Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/types.md Demonstrates how to instantiate and use the Metadata class to access various metadata properties of a data model. It shows retrieving table names, total size, and schema information. ```python from pbixray.loader import DataModelLoader from pbixray.meta import Metadata loader = DataModelLoader('report.pbix') metadata = Metadata(loader.data_model) print(metadata.tables) print(metadata.size) print(metadata.schema) ``` -------------------------------- ### Basic PBIXRay Usage Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/README.md Load a PBIX file, list tables, get a table as a DataFrame, and access schema and statistics. ```python from pbixray import PBIXRay # Load a PBIX file model = PBIXRay('report.pbix') # List all tables print(model.tables) # Get a table as DataFrame df = model.get_table('Sales') # Access metadata print(model.schema) print(model.statistics) print(model.dax_measures) ``` -------------------------------- ### Python: Complete Decode Flow Example Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/vertipaq-decoder-api.md Illustrates the end-to-end process of decoding a Power BI PBIX file using pbixray. It covers loading the PBIX, extracting metadata, initializing the VertiPaqDecoder, and finally decoding a specific table. ```python from pbixray.loader import DataModelLoader from pbixray.meta import Metadata from pbixray.vertipaq_decoder import VertiPaqDecoder # 1. Load and decompress PBIX loader = DataModelLoader('report.pbix') # 2. Load metadata metadata = Metadata(loader.data_model) # 3. Create decoder decoder = VertiPaqDecoder(metadata.source, loader.data_model) # 4. Decode a table df = decoder.get_table('Sales') ``` -------------------------------- ### DataModel Usage Example Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/types.md Demonstrates how to access properties of a DataModel instance after it has been loaded by DataModelLoader. Shows how to check container type, file log size, decompressed data size, and error/compression flags. ```python from pbixray.abf.data_model import DataModel, Container from pbixray.loader import DataModelLoader # Accessing during internal processing loader = DataModelLoader('report.pbix') dm = loader.data_model print(f"Container: {dm.container}") print(f"Files: {len(dm.file_log)}") print(f"Decompressed size: {len(dm.decompressed_data)} bytes") # Checking error conditions if dm.error_code: print("Error code detected") if dm.apply_compression: print("File-level compression applied") ``` -------------------------------- ### Get TMSCHEMA_PARTITIONS DMV Equivalent Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses partition information equivalent to the TMSCHEMA_PARTITIONS DMV. ```python model = PBIXRay('report.pbix') print(model.tmschema_partitions) ``` -------------------------------- ### VertiPaqDecoder Example Usage Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/types.md Demonstrates how to initialize and use the VertiPaqDecoder to retrieve a DataFrame for a specific table. This class is typically used internally by PBIXRay. ```python from pbixray.loader import DataModelLoader from pbixray.meta import Metadata from pbixray.vertipaq_decoder import VertiPaqDecoder loader = DataModelLoader('report.pbix') metadata = Metadata(loader.data_model) dealer = VertiPaqDecoder(metadata.source, loader.data_model) # Decode a table df = decoder.get_table('Sales') print(df.head()) ``` -------------------------------- ### Load PBIX and Access Metadata Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/metadata-sources-api.md Demonstrates loading a PBIX file using DataModelLoader and accessing its metadata through the Metadata object. It shows how to get the schema, refresh policies, role memberships, and segment metadata for a column. The SqliteMetadataSource is used automatically for PBIX files. ```python from pbixray.loader import DataModelLoader from pbixray.meta import Metadata # Load PBIX (will use SqliteMetadataSource automatically) loader = DataModelLoader('report.pbix') metadata = Metadata(loader.data_model) # Access metadata source source = metadata.source # Get schema print(source.schema_df) # Get TMSCHEMA_* data print(source.refresh_policies_df) print(source.role_memberships_df) # Extract segment metadata for a column col_row = source.schema_df.iloc[0] segments = source.get_segment_meta(col_row) print(f"Column has {len(segments)} segments") # Load XLSX (will use XmlMetadataSource automatically) loader2 = DataModelLoader('workbook.xlsx') metadata2 = Metadata(loader2.data_model) # Same interface; PBIX-only properties are empty print(len(metadata2.source.m_df)) # 0 (empty) print(len(metadata2.source.schema_df)) # > 0 ``` -------------------------------- ### Get TMSCHEMA_KPIS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_KPIS DMV. ```python @property def tmschema_kpis() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_PERSPECTIVE_TABLES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_PERSPECTIVE_TABLES DMV. ```python @property def tmschema_perspective_tables() -> pd.DataFrame ``` -------------------------------- ### Query All Model Information Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Retrieves all columns from the Model table. Use this to get a general overview of the model's properties. ```sql SELECT * FROM Model; ``` -------------------------------- ### Get Model Metadata Source: https://github.com/hugoberry/pbixray/blob/main/README.md Extract and print metadata related to the Power BI configuration used during model creation. ```python metadata = model.metadata print(metadata) ``` -------------------------------- ### List All Columns with Details Source: https://github.com/hugoberry/pbixray/blob/main/README.md This snippet lists all columns, including their table name, data type, and hidden status. Use this to get a comprehensive overview of the model's structure. ```python print(model.tmschema_columns[["TableName", "Name", "DataType", "IsHidden"]]) ``` -------------------------------- ### Handle OutOfBounds Datetime Example Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/errors.md Demonstrates how PBIXRay handles pandas OutOfBoundsDatetime errors by falling back to second precision for dates outside the nanosecond range. No user-facing exception is raised. ```python model = PBIXRay('report.pbix') # If a Date column contains year 8525 (observed in DAX examples): df = model.get_table('Sales') # Column will have datetime64[s] dtype instead of datetime64[ns] ``` -------------------------------- ### Retrieve Variations Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Fetches details about variations, joining with Column and Table to get column and table names. ```sql SELECT v.ID, v.ColumnID, COALESCE(c.ExplicitName, c.InferredName) AS ColumnName, t.Name AS TableName, v.Name, v.Description, v.RelationshipID, v.DefaultHierarchyID, v.DefaultColumnID, v.IsDefault FROM Variation v JOIN [Column] c ON v.ColumnID = c.ID JOIN [Table] t ON c.TableID = t.ID; ``` -------------------------------- ### Retrieve Perspective Tables Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Selects details of tables included in perspectives, joining PerspectiveTable with Perspective and Table to get names. ```sql SELECT pt.ID, p.Name AS PerspectiveName, pt.PerspectiveID, t.Name AS TableName, pt.TableID, pt.IncludeAll, pt.ModifiedTime FROM PerspectiveTable pt JOIN Perspective p ON pt.PerspectiveID = p.ID JOIN [Table] t ON pt.TableID = t.ID; ``` -------------------------------- ### Retrieve KPIs Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Selects detailed information about Key Performance Indicators (KPIs), joining KPI with Measure and Table to get associated names. ```sql SELECT k.ID, m.Name AS MeasureName, k.MeasureID, t.Name AS TableName, k.Description, k.TargetDescription, k.TargetExpression, k.TargetFormatString, k.StatusGraphic, k.StatusDescription, k.StatusExpression, k.TrendGraphic, k.TrendDescription, k.TrendExpression, k.ModifiedTime FROM KPI k JOIN Measure m ON k.MeasureID = m.ID JOIN [Table] t ON m.TableID = t.ID; ``` -------------------------------- ### Get TMSCHEMA_DATASOURCES DMV Equivalent Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses data source information equivalent to the TMSCHEMA_DATASOURCES DMV. ```python model = PBIXRay('report.pbix') print(model.tmschema_datasources) ``` -------------------------------- ### Get DAX Calculated Tables Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves all DAX calculated tables from the PBIX file. Returns a DataFrame with table names and expressions. ```python model = PBIXRay('report.pbix') print(model.dax_tables) ``` -------------------------------- ### Get Model Size Source: https://github.com/hugoberry/pbixray/blob/main/README.md Calculate and print the total size of the Power BI model in bytes. ```python size = model.size print(f"Model size: {size} bytes") ``` -------------------------------- ### Get All Table Names Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Access the list of all table names present in the data model. System tables are excluded from this list. ```python model = PBIXRay('report.pbix') table_names = model.tables print(table_names) # ['Sales', 'Products', 'Customers'] ``` -------------------------------- ### Get Data Model Relationships Source: https://github.com/hugoberry/pbixray/blob/main/README.md Retrieve and print details about the data model relationships. The output is a dataframe with relationship metadata. ```python relationships = model.relationships print(relationships) ``` -------------------------------- ### Initialize PBIXRay Model Source: https://github.com/hugoberry/pbixray/blob/main/pbixray.ipynb Load a PBIX file by creating an instance of the PBIXRay class. ```python from pbixray import PBIXRay model = PBIXRay('Adventure Works DW 2020.pbix') ``` -------------------------------- ### Get DAX Calculated Columns Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves all DAX calculated columns from the PBIX file. Returns a DataFrame with column names and expressions. ```python model = PBIXRay('report.pbix') print(model.dax_columns) ``` -------------------------------- ### Get TMSCHEMA_TABLES DMV Equivalent Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses full table metadata equivalent to the TMSCHEMA_TABLES DMV. Use .tables for a simple name list. ```python model = PBIXRay('report.pbix') print(model.tmschema_tables) ``` -------------------------------- ### Get TMSCHEMA_CALENDAR_COLUMN_REFERENCES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_CALENDAR_COLUMN_REFERENCES DMV. ```python @property def tmschema_calendar_column_refs() -> pd.DataFrame ``` -------------------------------- ### Get Model Statistics Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves storage statistics for columns in the data model. Returns a DataFrame with cardinality and size information. ```python model = PBIXRay('report.pbix') stats = model.statistics print(stats) # TableName ColumnName Cardinality Dictionary HashIndex DataSize # 0 Sales Amount 100 2048 4096 8192 ``` -------------------------------- ### Get TMSCHEMA_CALENDAR_COLUMN_GROUPS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_CALENDAR_COLUMN_GROUPS DMV. ```python @property def tmschema_calendar_column_groups() -> pd.DataFrame ``` -------------------------------- ### Initialize PBIXRay with File Path Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/configuration.md Instantiate the PBIXRay class by providing the path to the PBIX or XLSX file. The path can be absolute or relative. ```python from pbixray import PBIXRay # Absolute path model = PBIXRay('/home/user/reports/sales.pbix') # Relative path model = PBIXRay('reports/sales.pbix') # Windows path model = PBIXRay('C:\\Users\\User\\Documents\\report.pbix') ``` -------------------------------- ### Get Model Statistics Source: https://github.com/hugoberry/pbixray/blob/main/README.md Retrieve and print statistics about the model, including column cardinality and component byte sizes. The output is a dataframe with detailed statistics. ```python statistics = model.statistics print(statistics) ``` -------------------------------- ### Get TMSCHEMA_CALENDARS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_CALENDARS DMV. ```python @property def tmschema_calendars() -> pd.DataFrame ``` -------------------------------- ### Initialize PBIXRay with Default Settings Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/configuration.md Instantiate the PBIXRay class without any specific configuration to use all default settings for parsing PBIX files. This is useful for basic analysis where default type detection and full decompression are acceptable. ```python from pbixray import PBIXRay # No configuration — all defaults model = PBIXRay('report.pbix') ``` -------------------------------- ### Get TMSCHEMA_FUNCTIONS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_FUNCTIONS DMV. ```python @property def tmschema_functions() -> pd.DataFrame ``` -------------------------------- ### Get Row-Level Security (RLS) Details Source: https://github.com/hugoberry/pbixray/blob/main/README.md Retrieve and print details about Row-Level Security roles and permissions. The output is a dataframe with RLS configuration. ```python rls = model.rls print(rls) ``` -------------------------------- ### Decode HIDX Column Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/architecture.md Example of decoding a numeric column that uses HIDX (hash index) and IDF. This process involves reading raw numeric data from IDF, applying scaling (BaseId, Magnitude), and special handling for types like Currency. ```python # Column: numeric currency amounts # File references: HIDX, IDF, (no Dictionary) # 1. Read IDF (numeric indexes or data) idf_data = get_data_slice(data_model, 'Column/Amount/IDF') values = _read_rle_bit_packed_hybrid(idf_data, segments_meta) # Result: [10000, 25000, 15000, ...] (raw integer data) # 2. Apply BaseId and Magnitude metadata: BaseId=0, Magnitude=10000 adjusted = pd.Series(values).add(0) / 10000 # Result: [1.0, 2.5, 1.5, ...] # 3. Special handling for Currency adjusted = adjusted.apply(lambda x: Decimal(x) / 10000) # Result: [Decimal('1'), Decimal('2.5'), Decimal('1.5'), ...] # 4. Keep as object dtype (Decimal) ``` -------------------------------- ### Get TMSCHEMA_DETAIL_ROWS_DEFINITIONS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_DETAIL_ROWS_DEFINITIONS DMV. ```python @property def tmschema_detail_rows_definitions() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_SETS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_SETS DMV. ```python @property def tmschema_sets() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_ATTRIBUTE_HIERARCHIES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_ATTRIBUTE_HIERARCHIES DMV. ```python @property def tmschema_attribute_hierarchies() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_VARIATIONS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_VARIATIONS DMV. ```python @property def tmschema_variations() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_CALCULATION_EXPRESSIONS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_CALCULATION_EXPRESSIONS DMV. ```python @property def tmschema_calculation_expressions() -> pd.DataFrame ``` -------------------------------- ### Initialize PBIXRay Source: https://github.com/hugoberry/pbixray/blob/main/README.md Import the PBIXRay module and initialize it with the path to your PBIX or XLSX file. This creates a model object for further analysis. ```python from pbixray import PBIXRay model = PBIXRay('path/to/your/file.pbix') ``` -------------------------------- ### Load and Inspect a PBIX File Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/README.md Load a PBIX file and inspect its size, tables, schema, and statistics. ```python from pbixray import PBIXRay model = PBIXRay('sales_report.pbix') # Check model size print(f"Model size: {model.size / 1_000_000:.1f} MB") # List tables print(f"Tables: {list(model.tables)}") # View schema print(model.schema) # Get statistics (cardinality, storage size) print(model.statistics) ``` -------------------------------- ### Get TMSCHEMA_CALCULATION_ITEMS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_CALCULATION_ITEMS DMV. ```python @property def tmschema_calculation_items() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_CALCULATION_GROUPS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_CALCULATION_GROUPS DMV. ```python @property def tmschema_calculation_groups() -> pd.DataFrame ``` -------------------------------- ### Initialize PBIXRay Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Instantiate PBIXRay to parse PBIX or XLSX files containing Power BI data models. Ensure the file path is correct and the file contains the necessary data model streams. ```python from pbixray import PBIXRay # Load a PBIX file model = PBIXRay('path/to/report.pbix') # Or load an XLSX file with PowerPivot model = PBIXRay('path/to/workbook.xlsx') ``` -------------------------------- ### PBIXRay Constructor Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Initializes a PBIXRay instance to parse and analyze a PBIX or XLSX file. The file must contain a Power BI data model. ```APIDOC ## PBIXRay Constructor ### Description Initializes a PBIXRay instance to parse and analyze a PBIX or XLSX file. The file must contain a Power BI data model. ### Method __init__ ### Parameters #### Path Parameters - **file_path** (str) - Required - Path to the PBIX or XLSX file to parse. File must contain a DataModel stream (PBIX) or xl/model/item.data (XLSX with PowerPivot). ### Raises - **RuntimeError**: No supported data model file found in the archive. File must be a PBIX with 'DataModel' or XLSX with 'xl/model/item.data'. - **FileNotFoundError**: File does not exist. - **zipfile.BadZipFile**: File is not a valid ZIP archive. ### Example ```python from pbixray import PBIXRay # Load a PBIX file model = PBIXRay('path/to/report.pbix') # Or load an XLSX file with PowerPivot model = PBIXRay('path/to/workbook.xlsx') ``` ``` -------------------------------- ### Get TMSCHEMA_QUERY_GROUPS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_QUERY_GROUPS DMV. ```python @property def tmschema_query_groups() -> pd.DataFrame ``` -------------------------------- ### Initialize VertiPaqDecoder Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/vertipaq-decoder-api.md Instantiate the VertiPaqDecoder with metadata and data model sources. Ensure DataModelLoader and Metadata are properly initialized. ```python from pbixray.loader import DataModelLoader from pbixray.meta import Metadata from pbixray.vertipaq_decoder import VertiPaqDecoder loader = DataModelLoader('report.pbix') metadata = Metadata(loader.data_model) decoder = VertiPaqDecoder(metadata.source, loader.data_model) ``` -------------------------------- ### Get TMSCHEMA_LINGUISTIC_METADATA Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_LINGUISTIC_METADATA DMV. ```python @property def tmschema_linguistic_metadata() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_OBJECT_TRANSLATIONS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_OBJECT_TRANSLATIONS DMV. ```python @property def tmschema_translations() -> pd.DataFrame ``` -------------------------------- ### SqliteMetadataSource Constructor Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/types.md Initializes SqliteMetadataSource to load metadata from a PBIX file's SQLite database. ```python class SqliteMetadataSource: def __init__(self, data_model: DataModel) ``` -------------------------------- ### Get TMSCHEMA_CULTURES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_CULTURES DMV. ```python @property def tmschema_cultures() -> pd.DataFrame ``` -------------------------------- ### Metadata Class Initialization Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/types.md Shows the constructor for the Metadata class, which acts as a facade for loading and accessing metadata from a DataModel. It takes a DataModel instance as input. ```python from pbixray.abf.data_model import DataModel class Metadata: def __init__(self, data_model: DataModel) ``` -------------------------------- ### Get TMSCHEMA_EXTENDED_PROPERTIES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_EXTENDED_PROPERTIES DMV. ```python @property def tmschema_extended_properties() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_ANNOTATIONS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_ANNOTATIONS DMV. ```python @property def tmschema_annotations() -> pd.DataFrame ``` -------------------------------- ### Initialize DataModelLoader Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/data-model-loader-api.md Loads a PBIX or XLSX file and decompress its data model. Access the parsed data model via the 'data_model' property. ```python from pbixray.loader import DataModelLoader # Load and decompress a PBIX file loader = DataModelLoader('report.pbix') # Access the decompressed data model data_model = loader.data_model print(f"File count: {len(data_model.file_log)}") print(f"Total size: {data_model.decompressed_data.__sizeof__()} bytes") ``` -------------------------------- ### Get TMSCHEMA_PERSPECTIVE_MEASURES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_PERSPECTIVE_MEASURES DMV. ```python @property def tmschema_perspective_measures() -> pd.DataFrame ``` -------------------------------- ### SqliteMetadataSource Constructor Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/metadata-sources-api.md Initializes SqliteMetadataSource by loading all metadata DataFrames from the PBIX file's embedded SQLite database. Time-related columns are automatically converted. ```python SqliteMetadataSource(data_model: DataModel) ``` -------------------------------- ### Get TMSCHEMA_PERSPECTIVE_HIERARCHIES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_PERSPECTIVE_HIERARCHIES DMV. ```python @property def tmschema_perspective_hierarchies() -> pd.DataFrame ``` -------------------------------- ### Get TMSCHEMA_PERSPECTIVE_COLUMNS Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_PERSPECTIVE_COLUMNS DMV. ```python @property def tmschema_perspective_columns() -> pd.DataFrame ``` -------------------------------- ### Handle PBIXRay File Errors Gracefully Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/README.md Demonstrates robust error handling for common issues when loading PBIX files with PBIXRay, such as FileNotFoundError, invalid file formats, or corrupted files. ```python from pbixray import PBIXRay try: model = PBIXRay('report.pbix') df = model.get_table('Sales') except FileNotFoundError: print("File not found") except RuntimeError as e: if "No supported data model" in str(e): print("Not a valid PBIX or XLSX file") else: raise except ValueError as e: if "Decompression size mismatch" in str(e): print("File appears corrupted") else: raise except KeyError: print("Table not found") ``` -------------------------------- ### Get TMSCHEMA_PERSPECTIVES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_PERSPECTIVES DMV. ```python @property def tmschema_perspectives() -> pd.DataFrame ``` -------------------------------- ### PBIX-Only Properties Initialization Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/metadata-sources-api.md Initializes DataFrames for PBIX-specific properties that lack direct XML equivalents. These are typically set to empty DataFrames. ```python m_df, m_parameters_df, metadata_df, rls_df, model_df, tables_df, columns_df, partitions_df, hierarchies_df, levels_df, datasources_df, perspectives_df, perspective_tables_df, perspective_columns_df, perspective_hierarchies_df, perspective_measures_df, kpis_df, annotations_df, extended_properties_df, cultures_df, translations_df, linguistic_metadata_df, query_groups_df, calculation_groups_df, calculation_items_df, calculation_expressions_df, variations_df, attribute_hierarchies_df, sets_df, refresh_policies_df, detail_rows_definitions_df, format_string_definitions_df, functions_df, calendars_df, calendar_column_groups_df, calendar_column_refs_df, alternate_of_df, related_column_details_df, group_by_columns_df, binding_info_df, analytics_ai_metadata_df, data_coverage_definitions_df, role_memberships_df ``` -------------------------------- ### Initialize XmlMetadataSource Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/metadata-sources-api.md Loads metadata from XML documents in XLSX Power Pivot models. The constructor parses various XML files including cube.xml, dimension files, and mdxscript.xml. ```python XmlMetadataSource(data_model: DataModel) # Parameters # ---------- # data_model : DataModel # Parsed data model with decompressed data. Must contain XML metadata files (xl/model/). # Initialization # --------------- # Upon construction: # 1. Parses cube.xml (model definition) # 2. Parses dimension XML files # 3. Parses partition XML files # 4. Parses measure group XML files # 5. Parses mdxscript.xml (if present) # 6. Parses datasource XML files # 7. Parses datasourceview XML files # 8. Extracts TBL object metadata # 9. Builds schema DataFrame # 10. Builds DAX tables, measures, columns # 11. Builds relationships # 12. Populates PBIX-only properties with empty DataFrames ``` -------------------------------- ### Get TMSCHEMA_LEVELS DMV Equivalent Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses level information equivalent to the TMSCHEMA_LEVELS DMV. ```python model = PBIXRay('report.pbix') print(model.tmschema_levels) ``` -------------------------------- ### Get TMSCHEMA_HIERARCHIES DMV Equivalent Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses hierarchy information equivalent to the TMSCHEMA_HIERARCHIES DMV. ```python model = PBIXRay('report.pbix') print(model.tmschema_hierarchies) ``` -------------------------------- ### PBIXRay (Main Class) API Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/README.md This section details the primary PBIXRay class, including its constructor and various methods and properties for accessing different aspects of a Power BI data model. ```APIDOC ## PBIXRay (Main Class) ### Description Provides the main interface for loading and interacting with PBIX/XLSX files. ### Methods and Properties | Member | Type | Purpose | |---|---|---| | `__init__(file_path)` | Constructor | Load PBIX/XLSX file | | `get_table(table_name)` | Method | Decode and return table as DataFrame | | `tables` | Property | List of table names | | `schema` | Property | TableName, ColumnName, PandasDataType | | `size` | Property | Total data model size in bytes | | `statistics` | Property | Cardinality and storage stats | | `metadata` | Property | Power BI configuration | | `power_query` | Property | M code for all queries | | `m_parameters` | Property | M parameter definitions | | `dax_tables` | Property | DAX calculated tables | | `dax_measures` | Property | DAX measure definitions | | `dax_columns` | Property | DAX calculated columns | | `relationships` | Property | Model relationships | | `rls` | Property | Row-level security rules | | `tmschema_*` | Properties (41) | TMSCHEMA_* DMV equivalents | ``` -------------------------------- ### Get TMSCHEMA_MODEL DMV Equivalent Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses model-level metadata equivalent to the TMSCHEMA_MODEL DMV. ```python model = PBIXRay('report.pbix') print(model.tmschema_model) ``` -------------------------------- ### Handle File Not Found Error Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/errors.md Check if the PBIX file exists before initializing PBIXRay to prevent FileNotFoundError. This ensures the specified file path is valid. ```python from pbixray import PBIXRay import os file_path = 'report.pbix' if not os.path.exists(file_path): print(f"File not found: {file_path}") else: model = PBIXRay(file_path) ``` -------------------------------- ### Display M Parameters Source: https://github.com/hugoberry/pbixray/blob/main/README.md Retrieve and print all M Parameters values in a dataframe. The dataframe includes columns for 'ParameterName', 'Description', 'Expression', and 'ModifiedTime'. ```python m_parameters = model.m_parameters print(m_parameters) ``` -------------------------------- ### m_parameters Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves all M Parameters values from the PBIX file. Returns a pandas DataFrame with columns: ParameterName, Description, Expression, and ModifiedTime. ```APIDOC ## m_parameters ### Description Retrieves all M Parameters values from the PBIX file. Returns a pandas DataFrame with columns: `ParameterName`, `Description`, `Expression`, and `ModifiedTime`. ### Returns A pandas DataFrame with M parameter definitions. ### Example ```python model = PBIXRay('report.pbix') print(model.m_parameters) ``` ``` -------------------------------- ### Get TMSCHEMA_COLUMNS DMV Equivalent Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses complete column definitions equivalent to the TMSCHEMA_COLUMNS DMV. ```python model = PBIXRay('report.pbix') print(model.tmschema_columns) ``` -------------------------------- ### Retrieve All Annotations Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Fetches all records from the Annotation table. ```sql SELECT * FROM Annotation; ``` -------------------------------- ### Get TMSCHEMA_REFRESH_POLICIES Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves data equivalent to the TMSCHEMA_REFRESH_POLICIES DMV. Use for inspecting incremental refresh policies. ```python @property def tmschema_refresh_policies() -> pd.DataFrame ``` -------------------------------- ### Configure Logging for PBIXRay Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/configuration.md Control PBIXRay's logging output using Python's built-in logging module. You can set the global logging level or specifically configure the 'pbixray' logger. ```python import logging # Enable debug logging logging.basicConfig(level=logging.DEBUG) # Or configure just pbixray logger = logging.getLogger('pbixray') logger.setLevel(logging.DEBUG) from pbixray import PBIXRay model = PBIXRay('report.pbix') # Will now emit debug logs ``` -------------------------------- ### Get Model Relationships Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Retrieves all relationships defined in the data model. Returns a DataFrame describing relationships between tables. ```python model = PBIXRay('report.pbix') rels = model.relationships print(rels[['FromTableName', 'ToTableName', 'IsActive']]) ``` -------------------------------- ### Retrieve Format String Definitions Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Selects all records from the FormatStringDefinition table. ```sql SELECT * FROM FormatStringDefinition; ``` -------------------------------- ### Retrieve Perspective Hierarchies Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Selects hierarchies included in perspectives, joining PerspectiveHierarchy with related tables to get names. ```sql SELECT ph.ID, pt.PerspectiveID, p.Name AS PerspectiveName, ph.HierarchyID, h.Name AS HierarchyName, t.Name AS TableName, ph.ModifiedTime FROM PerspectiveHierarchy ph JOIN PerspectiveTable pt ON ph.PerspectiveTableID = pt.ID JOIN Perspective p ON pt.PerspectiveID = p.ID JOIN Hierarchy h ON ph.HierarchyID = h.ID JOIN [Table] t ON h.TableID = t.ID; ``` -------------------------------- ### XmlMetadataSource Constructor Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/types.md Initializes XmlMetadataSource to load metadata from XML documents in XLSX files. ```python class XmlMetadataSource: def __init__(self, data_model: DataModel) ``` -------------------------------- ### List Tables in PBIX Source: https://github.com/hugoberry/pbixray/blob/main/pbixray.ipynb Access the tables property to retrieve a list of all tables available in the loaded model. ```python model.tables ``` -------------------------------- ### Get TMSCHEMA_DATA_COVERAGE_DEFINITIONS DMV Data Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses the TMSCHEMA_DATA_COVERAGE_DEFINITIONS DMV. This property returns a pandas DataFrame containing the relevant data. ```python def tmschema_data_coverage_definitions() -> pd.DataFrame: pass ``` -------------------------------- ### Get TMSCHEMA_ANALYTICS_AI_METADATA DMV Data Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses the TMSCHEMA_ANALYTICS_AI_METADATA DMV. This property returns a pandas DataFrame containing the relevant data. ```python def tmschema_analytics_ai_metadata() -> pd.DataFrame: pass ``` -------------------------------- ### Retrieve Binding Info Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Selects all records from the BindingInfo table. ```sql SELECT * FROM BindingInfo; ``` -------------------------------- ### Retrieve All Perspectives Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Fetches all records from the Perspective table. ```sql SELECT * FROM Perspective; ``` -------------------------------- ### Get TMSCHEMA_BINDING_INFO DMV Data Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses the TMSCHEMA_BINDING_INFO DMV. This property returns a pandas DataFrame containing the relevant data. ```python def tmschema_binding_info() -> pd.DataFrame: pass ``` -------------------------------- ### List Tables in Model Source: https://github.com/hugoberry/pbixray/blob/main/README.md Retrieve and print a list of all tables present in the Power BI model. ```python tables = model.tables print(tables) ``` -------------------------------- ### Get TMSCHEMA_GROUP_BY_COLUMNS DMV Data Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses the TMSCHEMA_GROUP_BY_COLUMNS DMV. This property returns a pandas DataFrame containing the relevant data. ```python def tmschema_group_by_columns() -> pd.DataFrame: pass ``` -------------------------------- ### XmlMetadataSource Constructor Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/metadata-sources-api.md Initializes XmlMetadataSource to load metadata from XML documents in XLSX Power Pivot models. It parses various XML files and builds the schema and DAX structures. ```APIDOC ## XmlMetadataSource ### Description Loads metadata from XML documents in XLSX Power Pivot models. ### Constructor `XmlMetadataSource(data_model: DataModel)` ### Parameters #### Path Parameters - **data_model** (DataModel) - Required - Parsed data model with decompressed data. Must contain XML metadata files (xl/model/). ### Initialization Upon construction: 1. Parses cube.xml (model definition) 2. Parses dimension XML files 3. Parses partition XML files 4. Parses measure group XML files 5. Parses mdxscript.xml (if present) 6. Parses datasource XML files 7. Parses datasourceview XML files 8. Extracts TBL object metadata 9. Builds schema DataFrame 10. Builds DAX tables, measures, columns 11. Builds relationships 12. Populates PBIX-only properties with empty DataFrames ``` -------------------------------- ### Get TMSCHEMA_RELATED_COLUMN_DETAILS DMV Data Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses the TMSCHEMA_RELATED_COLUMN_DETAILS DMV. This property returns a pandas DataFrame containing the relevant data. ```python def tmschema_related_column_details() -> pd.DataFrame: pass ``` -------------------------------- ### Retrieve Data Coverage Definitions Source: https://github.com/hugoberry/pbixray/blob/main/docs/TMSCHEMA_MAPPING.md Fetches detailed information about data coverage definitions, including associated partition and table names. This query is useful for understanding data quality and coverage across different partitions and tables. ```sql SELECT dcd.ID, p.Name AS PartitionName, dcd.PartitionID, t.Name AS TableName, dcd.Description, dcd.Expression, dcd.State, dcd.ErrorMessage, dcd.ModifiedTime FROM DataCoverageDefinition dcd JOIN [Partition] p ON dcd.PartitionID = p.ID JOIN [Table] t ON p.TableID = t.ID; ``` -------------------------------- ### Get TMSCHEMA_ALTERNATE_OF DMV Data Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Accesses the TMSCHEMA_ALTERNATE_OF DMV. This property returns a pandas DataFrame containing the relevant data. ```python def tmschema_alternate_of() -> pd.DataFrame: pass ``` -------------------------------- ### Handle Unsupported Data Model Compression Error Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/errors.md Catch RuntimeError for unsupported DataModel compression formats. This can help diagnose issues with corrupted or future-version PBIX files. ```python from pbixray import PBIXRay try: model = PBIXRay('report.pbix') except RuntimeError as e: if "Unknown or unsupported DataModel compression format" in str(e): print("DataModel cannot be decompressed") print("Try re-downloading the file or checking for corruption") ``` -------------------------------- ### metadata Source: https://github.com/hugoberry/pbixray/blob/main/_autodocs/pbixray-api-reference.md Returns a DataFrame containing metadata about the Power BI configuration used during model creation. ```APIDOC ## metadata ### Description Returns a DataFrame containing metadata about the Power BI configuration used during model creation. ### Method metadata ### Returns A pandas DataFrame with Power BI metadata. ### Example ```python model = PBIXRay('report.pbix') print(model.metadata) ``` ``` -------------------------------- ### Inspect Incremental Refresh Policies Source: https://github.com/hugoberry/pbixray/blob/main/README.md This snippet displays the incremental refresh policies configured for the model. This is useful for understanding how data is refreshed and managed. ```python print(model.tmschema_refresh_policies) ```