Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:22:29+00:00 2026-05-26T16:22:29+00:00

So I’ve run into an interesting problem. I need to specify a custom WHERE

  • 0

So I’ve run into an interesting problem. I need to specify a custom WHERE clause in a PowerPivot query. I must change it based on external conditions. I would like to edit the file and save a copy. Any idea how to do this? I opened the PowerPivot file from binary, but it appears encrypted…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:22:30+00:00Added an answer on May 26, 2026 at 4:22 pm

    Solution was to open the Excel workbook up as a Zip (using the Package class).

    If you are looking to modify the queries, you can. The file at /xl/customData/item1.data is a backup file that represents the PowerPivot database (which is just an Analysis Services database running in Vertipaq mode) used to process queries. You need to restore the file to a SSAS instance running in Vertipaq mode. Once that’s done, script the queries as an ALTER script. Modify the scripts (in this case, replacing @projectId with my actual projectID), then run them against the database. Once all this is done, back the database up and put back into the Excel workbook. That modifies the queries.

    The connection data is stored in the /xl/connections.xml file. Open that up, modify, and replace. Repack it all up again, and now you have a workbook again.

    Here’s the code I made. You will have to call the methods as you need. Basic idea is there, though…

        const string DBName = "Testing";
        const string OriginalBackupPath = @"\\MyLocation\BKUP.abf";
        const string ModifiedBackupPath = @"\\MyLocation\BKUPAfter.abf";
        const string ServerPath = @"machineName\powerpivot";
    
        private static readonly Server srv = new Server();
        private static readonly Scripter scripter = new Scripter();
        private static Database db;
    
        private static byte[] GetPackagePartContents(string packagePath, string partPath)
        {
            var pack = Package.Open(packagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            var part = pack.GetPart(new Uri(partPath, UriKind.Relative));
            var stream = part.GetStream();
            var b = new byte[stream.Length];
            stream.Read(b, 0, b.Length);
            stream.Flush();
            stream.Close();
            pack.Flush();
            pack.Close();
            return b;
        }
    
        private static void WritePackagePartContents(string packagePath, string partPath, byte[] contents)
        {
            var uri = new Uri(partPath, UriKind.Relative);
            var pack = Package.Open(packagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            var part = pack.GetPart(uri);
            var type = part.ContentType;
            pack.DeletePart(uri);
            pack.CreatePart(uri, type);
            part = pack.GetPart(uri);
            var stream = part.GetStream();
            stream.Write(contents, 0, contents.Length);
            stream.Flush();
            stream.Close();
            pack.Flush();
            pack.Close();
        }
    
        private static void RestoreBackup(string server, string dbName, string backupPath)
        {
            srv.Connect(server);
            if (srv.Databases.FindByName(dbName) != null) { srv.Databases.FindByName(dbName).Drop(); srv.Update(); }
            srv.Restore(backupPath, dbName, true);
            srv.Update();
            srv.Refresh();
        }
    
        private static void WriteContentsToFile(byte[] contents, string filePath)
        {
            var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            fileStream.Write(contents, 0, contents.Length);
            fileStream.Flush();
            fileStream.Close();
        }
    
        private static byte[] ReadContentsFromFile(string filePath)
        {
            var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            var b = new byte[fileStream.Length];
            fileStream.Read(b, 0, b.Length);
            fileStream.Flush();
            fileStream.Close();
            return b;
        }
    
        private static XDocument GetAlterScript(MajorObject obj)
        {
            var stream = new MemoryStream();
            var streamWriter = XmlWriter.Create(stream);
            scripter.ScriptAlter(new[] { obj }, streamWriter, false);
            streamWriter.Flush();
            streamWriter.Close();
            stream.Flush();
            stream.Position = 0;
            var b = new byte[stream.Length];
            stream.Read(b, 0, b.Length);
            var alterString = new string(Encoding.UTF8.GetString(b).ToCharArray().Where(w => w != 65279).ToArray());
            var alter = XDocument.Parse(alterString);
            stream.Close();
            return alter;
        }
    
        private static void ExecuteScript(string script)
        {
            srv.Execute(script);
            srv.Update();
            db.Process();
            db.Refresh();
        }
    
    
    
        private static void ProcessPowerpointQueries(string bookUrl, string projectId)
        {
            byte[] b = GetPackagePartContents(bookUrl, "/xl/customData/item1.data");
            WriteContentsToFile(b, OriginalBackupPath);
            RestoreBackup(ServerPath, DBName, OriginalBackupPath);
            var db = srv.Databases.GetByName(DBName);
            var databaseView = db.DataSourceViews.FindByName("Sandbox");
            var databaseViewAlter = GetAlterScript(databaseView);
            var cube = db.Cubes.FindByName("Sandbox");
            var measureGroup = cube.MeasureGroups.FindByName("Query");
            var partition = measureGroup.Partitions.FindByName("Query");
            var partitionAlter = GetAlterScript(partition);
            var regex = new Regex(@"\s@projectid=\w*[ ,]");
            var newDatabaseViewAlter = databaseViewAlter.ToString().Replace(regex.Match(databaseViewAlter.ToString()).Value.Trim(',',' '), @"@projectid=" + projectId);
            ExecuteScript(newDatabaseViewAlter);
            var newPartitionAlter = partitionAlter.ToString().Replace(regex.Match(partitionAlter.ToString()).Value.Trim(',', ' '), @"@projectid=" + projectId);
            ExecuteScript(newPartitionAlter);
            db.Backup(ModifiedBackupPath, true);
            WritePackagePartContents(bookUrl, @"/xl/customData/item1.data", ReadContentsFromFile(ModifiedBackupPath));
            db.Drop();
            srv.Disconnect();
        }
    
        private static void ProcessWorkbookLinks(string bookUrl, string newCoreUrl)
        {
            var connectionsFile = GetPackagePartContents(bookUrl, @"/xl/connections.xml");
            var connectionsXml = Encoding.UTF8.GetString(connectionsFile);
            connectionsXml = connectionsXml.Replace(
                new Regex(@"Data Source=\S*;").Match(connectionsXml).Value.Trim(';'), @"Data Source=" + newCoreUrl);
            WritePackagePartContents(bookUrl, @"/xl/connections.xml", connectionsXml.Replace(@"https://server/site/", newCoreUrl).ToCharArray().Select(Convert.ToByte).ToArray());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.