Private/New-AzureUtilsInheritedTagQuery.ps1

function New-AzureUtilsInheritedTagQuery {
    <#
        .SYNOPSIS
            Builds the Resource Graph (KQL) query that pairs resources with their
            resource-group tags.
        .DESCRIPTION
            Pure, side-effect-free. Inner-joins each resource to its parent resource
            group (in 'resourcecontainers') on subscription + lower-cased group name,
            projecting both the resource's own tags and the resource group's tags so
            the caller can compute which tags should be inherited. Only groups that
            carry at least one tag are returned.
        .OUTPUTS
            System.String (the KQL query)
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param()

    return @'
resources
| extend rgKey = tolower(resourceGroup)
| join kind=inner (
    resourcecontainers
    | where type =~ 'microsoft.resources/subscriptions/resourcegroups'
    | extend rgKey = tolower(name)
    | where isnotnull(tags) and array_length(bag_keys(tags)) > 0
    | project rgKey, subscriptionId, rgName = name, rgTags = tags
) on subscriptionId, rgKey
| project id, name, resourceGroup = rgName, subscriptionId, tags, rgTags
'@

}